IOS_OC_基础语法

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

self 既可以指代当前类也可以指代对象

当在+类函数中调用指代类方法,在-中调用指代成员方法

不允许重复定义成员变量,但是方法可以重写,优先调用子类方法

nimal *a = [Dog new];
//[a run]; a指针实际上指向的还是Dog的内存空间,该空间存在一个isa指针指向类,实际上执行的还是Dog对象的run方法

Class c = [Animal class];
Animal *a = [Animal new];

NSLog(@"%p", c);
NSLog(@"%p", [a class]);

[c show];// + show
[a show];// - show

函数与方法的区别:函数是属于文件的

@implementation Person
void s()
{
    //NSLog(@"s() invoke %d", _age); 不能访问类成员变量
    NSLog(@"s() invoke");
}
@end

int main(int argc, const char * argv[])
{
    s();
    return 0;
}

@interface Person : NSObject
{
    int _age;
}
- (void)setAge:(int)age;
- (void)show;
+ (void)log;
@end

@implementation Person

- (void)setAge:(int)age
{
    _age = age;
}

- (void)show
{
    NSLog(@"age = %d", _age);
}

+ (void)log
{
    NSLog(@"log information");
}

@end

int main(int argc, const char * argv[])
{
    Person *p = [Person new];
    [p setAge:100];
    [p show];
    //[Person show]; // error 认为Person 存在一个 +show方法
    [Person log];
    //[p log];       //error 认为Person 存在一个 -log方法
    
    return 0;
}

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

public 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_基础语法)