Translate

2012年9月25日 星期二

NSArray compare 排序

其實一般NSArray的排序

NSArray *sortedArray = [temp_dic_dic sortedArrayUsingSelector:@selector(compare:)]; for(int i =0; i < [sortedArray count]; i++) { int x = [[sortedArray objectAtIndex:i] intValue]; NSLog(@"x is %d",x); }

但是我需要的是針對NSArray多個dictionary,每個dictionary都有個key值為"dddNumber",key值的形態為字串,如何依照NSString比對呢?

NSMutableArray *array = [[NSMutableArray alloc] init]; for (int i = 0; i < 10; i ++) { NSDictionary *aDict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"akdj%i", i], @"phaseNumber", nil]; [array addObject:aDict]; [aDict release]; } // 排序 NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"dddNumber" ascending:YES] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
是要用intValue排序的话。上面代码是不对的,上面是按照String来排序的。 如果要按照intValue排序,参见如下:

方法
NSComparisonResult compare(NSDictionary *firstDict, NSDictionary *secondDict, void *context) {NSLog(@"IN(%i, %i)", [[firstDict objectForKey:@"ddd"] intValue], [[secondDict objectForKey:@"ddd"] intValue]); if ([[firstDict objectForKey:@"ddd"] intValue] < [[secondDict objectForKey:@"ddd"] intValue]) return NSOrderedAscending; else if ([[firstDict objectForKey:@"ddd"] intValue] > [[secondDict objectForKey:@"ddd"] intValue]) return NSOrderedDescending; else return NSOrderedSame; } [array sortUsingFunction:compare context:NULL];
使用
[array sortUsingFunction:compare context:NULL];

2012年9月5日 星期三

Application does not run in background

在專案的info.plist新增

Application does not run in background

設為YES


Where is the XCode 4 build directory?


Where is the XCode 4 build directory?

In all previous versions, XCode used to create a build directory inside the project folder and the executables were saved in that location.

However, XCode 4 changed all that.
Infact, I spent quite sometime searching for my executables.
XCode 4 uses the derived data location for storing your builds by default.

Just click on the XCode menu on the top bar, and select Preferences -> Locations tab.
Here under build location, you can see the default setting selected as "place build products in derived data location (recommended)".

And if you check the "Derived Data Location" section of this tab you will see the default location is /Users/..../Library/Developer/XCode/DerivedData.
You will see a small arrow next to this path and clicking on it takes you directly to this location and voila there is your build directory!

2012年8月29日 星期三

NSDateFormatter


部分日期格式的总结:
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mmaaa"];
NSDate *date = [NSDate date];
NSString * s = [dateFormatter stringFromDate:date];
显示效果为:10/05/2010 03:49PM

下面附上几个格式:这个是从Cocoachina找来的:
yyyy:MM:dd G 'at' HH:mm:ss zzz            1996.07.10 AD at 15:08:56 PDT
EEE, MMM d, "yy                                         Wed,july 10, '99
h:mm a                                                     12:08 PM
hh 'o"clock' a,zzzz                                      12 o'clock PM, Pacific Daylight Time
K:mm a, z                                                 0:00 PM, PST
yyyyy,MMMM.dd GGG hh:mm aaa                01996.july.10 AD 12:08 PM

下面是得到当前的年,月,日,时,分,秒:
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dd = [cal components:unitFlags fromDate:date];
int y = [dd year];
int m = [dd month];
int d = [dd day];
int h = [dd hour];
int m = [dd minute];
int s = [dd second];
日期格式代表意義
yyyy = 年(4位數), yy = 年(最後2位數),MM = 月(1~12),MMM = 月(英文縮寫),MMMM = 月(英文完整)
K = 時(12小時制),H = 時(24小時制),mm = 分,ss = 秒,aa = 午前/午後(AM/PM)
dd = 日,EEE = 星期(英文縮寫),EEEE = 星期(英文完整)

2012年8月22日 星期三

UIWebView backgroundColor

web.backgroundColor = [UIColor clearColor]; web.opaque = NO; 关键是在HTML里:

2012年7月20日 星期五

UIView的圓角邊邊

畫圓角
記得framework需要加QuartzCore.framework
並且

#import <QuartzCore/QuartzCore.h>


UIImageView *img = [[UIImageView alloc] initWithFrame:imgframe]; [img setImage:image]; img.layer.cornerRadius = 3; img.layer.masksToBounds = YES; img.layer.borderWidth = 1; img.layer.borderColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] CGColor];

2012年7月19日 星期四

UIImage to UIColor and UIColor to UIImage

UIImage *imgpattern = [UIImage imageNamed:@"box_bg.png"]; [info_sv setBackgroundColor:[UIColor colorWithPatternImage:imgpattern]];
- (UIImage *) createImageWithColor: (UIColor *) color { CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; }