码迷,mamicode.com
首页 > 移动开发 > 详细

IOS_OC_基础语法

时间:2014-06-11 06:19:36      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   java   

1.YES与NO

Object-c 提供了 BOOL 类型, 但这个BOOL 类型和 C++里的并不一样: 在C++里一切非 0 值的东西都 为 true,而为 0 值的为 false。但是 Object-c 里 1 为 true 并被宏定义为 YES=1,0 为 false 并被宏定义为 NO=0。

+ (BOOL)isEquals:(int)x with:(int)y
{
    return x - y;
}
if ([Provider isEquals:10 with:1021]) {// == YES //error
        NSLog(@" !!!!!!");
 } else {
        NSLog(@" ===== ");
 }

2.类CLass

在OC的一个类中,不允许重复定义一个函数,区分函数是否重复根据方法名,与接收参数无关。与java不同

pubic void getInfo(Animal a);
public void getInfo(Person p);

//同一个方法getInfo:
- (void)getInfo:(Animal)a;
- (void)getInfo:(Person)p;

3.常见的集合

void arrayTest()
{
    NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
    NSLog(@"len = %lu", [array count]);
    for (NSObject *obj in array) {
        NSLog(@"== %@", obj);
    }
    for (int i=0; i<[array count]; i++) {
        NSLog(@"%@", [array objectAtIndex:i]);
    }
    //写进文件,atomically是否先将文件保存至临时文件中,待保存成功之后,再与原始文件替换,是一种安全机制。
    [array writeToFile:@"/Users/zf/Desktop/1.txt" atomically:YES];
    //读取文件
    NSArray *arr = [NSArray arrayWithContentsOfFile:@"/Users/zf/Desktop/1.txt"];
    NSLog(@"len3 = %lu", [arr count]);
    
}
void mutableArrayTest()
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
    [array addObject:@"one"];
    [array addObject:@"two"];
    [array addObject:@"three"];
    [array addObject:@"two1"];
    
    NSLog(@"len = %lu", [array count]);
    [array removeObjectAtIndex:3];
    NSLog(@"len = %lu", [array count]);
    
    NSEnumerator *e = [array objectEnumerator];
    NSString *x;
    while (x = [e nextObject]) {
        NSLog(@"x == %@", x);
    }
    NSEnumerator *e1 = [array reverseObjectEnumerator];
}
void dictTest()
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
    NSLog(@"%@", [dict objectForKey:@"key2"]);
    
    NSMutableDictionary *d = [NSMutableDictionary dictionaryWithCapacity:3];
    
    [d setObject:@"wwwwww" forKey:@"key1"];
    [d removeObjectForKey:@"key1"];
    NSLog(@"%lu, key1=%@", [d count], [d objectForKey:@"key1"]);
}

NSArray:有序集合,元素在一个整块的内存中并按序排列;

NSSet:无序集合,散列存储。
读developer.apple关于NSSet的解释:You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.
搜索一个元素,NSSet的效率会比NSArray高。为什么呢?原理比较简单:NSSet中元素的存储和访问都是一个hash的过程。比如你要存储元素A,一个hash算法直接就能直接找到A应该存储的位置;同样,当你要访问A时,一个hash过程就能找到A存储的位置。而对于NSArray,若想知道A到底在不在数组中,则需要一个一个元素比较,显然效率不高。

4.NSString

void stringTest()
{
    NSString *str = [NSString stringWithFormat:@"aa,bb,cc,"];
    NSArray *array = [str componentsSeparatedByString:@","];
    NSLog(@"len = %lu", [array count]);
    
    NSLog(@"%@", [array componentsJoinedByString:@"-"]);
}

IOS_OC_基础语法,布布扣,bubuko.com

IOS_OC_基础语法

标签:des   style   class   blog   code   java   

原文地址:http://blog.csdn.net/zimo2013/article/details/26563493

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!