ios開發理解nil,Nil, NULL


nil是一個對象指針為空,Nil是一個類指針為空,NULL是基本數據類型為空。這些可以理解為nil,Nil, NULL的區別吧。

iOS剪切板

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

pasteboard.string = @"要賦給剪切板的字符串";

1 ID

可以傳遞任何消息給id,但如果該id不支持這個消息就會返回一個運行時異常,通常就是:“unrecognisedselector sent to instance to XXX” 消息。
 
2 SEL
SEL 類型也可以用NSSelectorFromString(NSString *)函數創建
 
nil 用來給對象賦值,
NULL 則給任何指針賦值,NULL 和 nil 不能互換,
nil 用於類指針賦值,而NSNull 則用於集合賦值,
如:
a.if (object == nil) {}//判斷對象為空
 
b.UIViewController *controller = [NSArray objectAtIndex:i];//判斷數組元素是否為空
if ((NSNull *)controller == [NSNull null]) {
//...
}
 
c.NSString *userId = [NSDictionary objectForKey:@"UserID"];//判斷字典對象的元素是否為空
if (userId == [NSNull null]) {
}
4 預處理宏
a 關閉調試信息:
#define DLog();
b
打印文件名,行號,函數詳情,函數名信息,
NSLog(@"%s %d %s",__FILE__, __LINE__,__PRETTY_FUNCTION__,__FUNCTION__);
#ifdef DEBUG
# define DLog(fmt,...) NSLog((@"%s [Line %d]" fmt), __PRETTY_FUNCTION__,__LINE__,##__VA_ARGS__);
#else
# define DLog(...);
#endif
 
5 自動釋放池(AutoReleasePool)
在程序中,當有大量的自動變量需要管理時,你就需要自行創建 NSAutoreleasePool來管理;
在創建線程或者使用NSOperation時,也需要創建獨立的NSAutoreasePool 來管理線程;
另外,重載didReceiveMemoryWarning()函數是一個好的編程習慣;
 
6 程序執行流程
所以流程應該是這樣:

(loadView/nib文件)來加載view到內存 ——>viewDidLoad函數進一步初始化這些view ——>內存不足時,調用viewDidUnload函數釋放views

—->當需要使用view時有回到第一步

如此循環
7 ASIHttpRequest 
http://www.dreamingwish.com/dream-2011/apples-third-party-development-libraries-asihttprequest.html
 
8 判斷一個字符串是否為空
  if (str == nil)
  if ([str length] == 0)
 
9 處理數值對象
  a. NSInteger   -------   int
    NSNumber *numObj = [NSNumber numberWithInt:2];
    NSInteger  myInteger = [numObj integerValue];
    int a = [myInteger intValue];
  
   b. 浮點數值使用CGFloat。NSDecimalNumber 對象進行處理
NSDecimalNumber *myDecimalObj = [[NSDecimalNumber allo] initWithString:@"23.39"];
NSLog(@"myDecimalObj doubleValue = %6.3f",[myDecimalObj doubleValue]);
CGFloat myCGFloatValue = 43.4;
NSDecimalNumber *myOtherDecimalObj = [[NSDecimalNumber alloc] initWithFloat:myCGFloatValue];
NSLog(@"myOtherDecimalObj doubleValue=%6.3f",[myOtherDecimalObj doubleValue]);
 10 處理日期時間NSDate
     a. 獲取當前日期時間的代碼如下
NSDate *dateToDay = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLocale *locale = [[NSlocale alloc] initWithLocalIdentifier:@"en_US"];
[df setLocale:locale];
     b. 從字符串生成日期對象的代碼如下
NSString *myDateString = @"2009-09-15 18:30:00";
NSDate *myDate = [df dateFromString: myDateString];
 
     c. 日期比較的代碼
switch ([dateToDay compare:myDate]) {
case NSOrderedSame:
break;
case NSOrderedAscending:
break;
case NSOrderedDescending:
break;
default:
break;
}
 
11  常用數組操作
  a 判斷數組中是否包含某個對象
  - (BOOL)containsObject:(id)anObject
b 增加、插入元素
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:anObject];
[array insertObject:anObject atIndex:2];
[array addObjectsFromArray:anotherArray];
c 獲取某個元素的索引值
NSInteger idx = [array indexOfObject:anObject];
d 更新數組元素
 [mutableArray replaceObjectAtIndex:idx withObject:[NSNumber numberWithInt:9]]
e 數組遍歷
1.使用枚舉
for (NSString *str in array) {
}
2 使用NSEnumerator
NSEnumerator *enumerator = [array objectEnumerator];
id obj;
for ( obj == [enumerator nextObject]) {
}
3.使用for循環
for (int i = 0; i < [array count]; i++) {
[array objectAtIndex:i];
}
 
12 字符串數組排序
a. NSArray *sortedArray = [array sortedArrayusingSelector:@selector(caseInsensitiveCompare:)];
b. NSCountedSet *cset = [[NSCountedSet alloc] initWithArray: array];
    NSArray *sorted = [[cset allObjects] sortedArrayUsingSelector:@selector(compare:)];
 
13 OC中產生隨機數的方法
srandom(time(NULL));
arc4random()%n;
 
14 數組map操作(-makeObjectsPerformSelector())
該函數可以將一個操作作用在數組中的所有元素上,如;
NSArray *fighters = ...;
[fighters makeObjectsPerformSelector:@selector(fly:)];
 
- (void)fly:(id)sender {
}
 
15 對象數組排序(使用NSSortDescriptor)
 
16 對象數組過濾 (使用 NSPredicate)
NSPredicate *aPredicate = [NSpredicate predicateWithFormat:@"SELF.lastName beginswith[c] 'a'"];
NSArray *array = [array filteredArrayUsingPredicate:aPredicate];
 
17 刪除數組中元素
一種更安全的方法,將滿足條件的元素放進一個臨時數組,再將這個數組返回,代碼如下:
- (NSArray *) filterPersonWithLastName:(NSString *)filterText {
Person *person = [Person alloc ] init];
NSMutableArray *personList = [person creatTempraryList];
NSLog(@"before");
NSMutableArray *personsToRemove = [NSMutableArray array];
for (Person *person in personList) {
if (filterText && [filterText rangeOfString:person.laseName options:NSLiteralSearch | NSCaseInsensitiveSearch].length == 0)
[personsToRemove  addObject:person];
}
[personList removeObjectsInArray:personsToRemove];
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM