源自:http://blog.youkuaiyun.com/kmyhy/archive/2010/07/06/5716755.aspx
1.使用class获得meta class
NSLog (@"Class name : %@",[[[arr objectAtIndex:i] class] description]);
2.使用NSClassFromString和 NSSelectorFromString
id object = [[ NSClassFromString (@ "NameofClass" ) alloc ] init ];// NSClassFromString加载的类不需要import SEL sel = NSSelectorFromString(@"doSomethingMethod:")//注意这个冒号,说明方法带有参数
if ([object respondsToSelector:sel]) {
[object performSelector:sel withObject:color]; //注意如果有两个参数,使用两个withObject:参数;
}
3.使用isa
id movie=[arrFavorite objectAtIndex :row];
if (movie->isa ==[IndexPageItem class ]) {
NSLog ( @"movie title:%@",[movie description ]);
}
4.谓词
NSArray * arr=[ NSArray arrayWithObjects : @"1", @"0", @"no", @"NO", @"YES", @"yes", nil ];
NSPredicate* predicate=[ NSPredicate predicateWithFormat :
@"SELF IN{'0','no','NO'}"];
NSArray * result=[arr filteredArrayUsingPredicate :predicate];
NSLog (@"%@" ,result);
for (NSString * s in arr){
NSLog( @"%@:%d" ,s,[predicate evaluateWithObject:s]);
}
5. From NSString to NSData
NSString * text = @ "Some string" ;
NSData * data = [ text dataUsingEncoding: NSUTF8StringEncoding] ;
6. From NSData to NSString
NSString * text = [ [ NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] ;
7.日期和时间
NSCalendar * gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSGregorianCalendar ];
NSDateComponents * todayComponents = [ gregorian components :( NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit ) yourDate ];
NSInteger theDay = [ todayComponents day ];
NSInteger theMonth = [ todayComponents month ];
NSInteger theYear = [ todayComponents year ];
// now build a NSDate object for yourDate using these components
NSDateComponents * components = [[ NSDateComponents alloc ] init ];
[ components setDay : theDay ];
[ components setMonth : theMonth ];
[ components setYear : theYear ];
NSDate * thisDate = [ gregorian dateFromComponents : components ];
[ components release ];
// now build a NSDate object for the next day
NSDateComponents * offsetComponents = [[ NSDateComponents alloc ] init ];
[ offsetComponents setDay : 1 ];
NSDate * nextDate = [ gregorian dateByAddingComponents : offsetComponents toDate : yourDate options : 0 ];
[ offsetComponents release ];
[ gregorian release ];
8.使用 performSelectorInBackground(多线程)调用的方法,必须 在该方法中 用 NSAutoreleasePool
否则出现错误: no pool in place - just leaking。如果用performSelector则没有这个问题。
9.ld: symbol(s) not found 错误的解决办法
展开"Targets-->Compile Sources",查看列出的所有.m文件,找出其中缺失的.m文件,拖到其中。
10.Ojbect C让线程休眠
[ NSThread sleepForTimeInterval: 2 ];//单位是秒
11.nil和NULL的区别
在Object C中,NULL和nil都是空指针,不同的是,NULL用于c 指针,即(void *);而nil用于c++或java对象,即id为空
12.字符串替换
str=[str stringByReplacingOccurrencesOfString: @"[]" withString: @"" ];
13.assign,copy,retain之间的区别
- assign : 简单赋值,不更改索引计数(Reference Counting)。
- copy : 建立一个索引计数为1的对象,然后释放旧对象
- retain :释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1
retain的实际语法为:
- ( void ) setName: ( NSString * ) newName {
if ( name != newName) {
[ name release] ;
name = [ newName retain] ;
// name’s retain count has been bumped up by 1
}
}
说了那么麻烦,其实接下来的话最重要:
?如果你不懂怎么使用他们,那么就这样 ->
- 使用assign: 对基础数据类型 (NSInteger,CGFloat)和C数据类型(int, float, double, char, 等等)
- 使用copy: 对NSString
- 使用retain: 对其他NSObject和其子类
14.nonatomic关键字:
atomic是Objc使用的一种线程保护技术,基本上来讲,是防止在写未完成的时候被另外一个线程读取,造成数据错误。而这种机制是耗费系统资源的,所以在iPhone这种小型设备上,如果没有使用多线程间的通讯编程,那么nonatomic是一个非常好的选择。
15.发送短信/邮件/打电话
+ (void)alert:(NSString *)msg
{
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:msg message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];
[alertView showWithBackground];
}
+ (NSString*) cleanPhoneNumber:(NSString*)phoneNumber
{
NSString* number = [NSString stringWithString:phoneNumber];
NSString* number1 = [[[number stringByReplacingOccurrencesOfString:@" " withString:@""]
// stringByReplacingOccurrencesOfString:@"-" withString:@""]
stringByReplacingOccurrencesOfString:@"(" withString:@""]
stringByReplacingOccurrencesOfString:@")" withString:@""];
return number1;
}
+ (void) makeCall:(NSString *)phoneNumber
{
if ([DeviceDetection isIPodTouch]){
[UIUtils alert:kCallNotSupportOnIPod];
return;
}
NSString* numberAfterClear = [UIUtils cleanPhoneNumber:phoneNumber];
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", numberAfterClear]];
NSLog(@"make call, URL=%@", phoneNumberURL);
[[UIApplication sharedApplication] openURL:phoneNumberURL];
}
+ (void) sendSms:(NSString *)phoneNumber
{
if ([DeviceDetection isIPodTouch]){
[UIUtils alert:kSmsNotSupportOnIPod];
return;
}
NSString* numberAfterClear = [UIUtils cleanPhoneNumber:phoneNumber];
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"sms:%@", numberAfterClear]];
NSLog(@"send sms, URL=%@", phoneNumberURL);
[[UIApplication sharedApplication] openURL:phoneNumberURL];
}
+ (void) sendEmail:(NSString *)phoneNumber
{
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", phoneNumber]];
NSLog(@"send sms, URL=%@", phoneNumberURL);
[[UIApplication sharedApplication] openURL:phoneNumberURL];
}
+ (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
{
NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
to, cc, subject, body];
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
@synchronized(enemiesArray) {
⋯⋯
⋯⋯
}
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(timerFired) userInfo:nil repeats:NO];
21、让UITextField里面的text垂直居中可以这样写:
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
22、在UITextField最右侧加图片是以下代码,
UIImageView *imgv=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=imgv;
text.rightViewMode = UITextFieldViewModeAlways;
如果是在最左侧加图片就换成:
text.leftView=imgv;
text.leftViewMode = UITextFieldViewModeAlways;