IOS_OC _ basic syntax

Source: Internet
Author: User

1. YES and NO

Object-c provides the BOOL type, but this BOOL type is not the same as that in C ++: In C ++, all non-0 values are true, the value 0 is false. However, in Object-c, 1 is true and the macro is defined as YES = 1, 0 is false, and the macro is defined as 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

In an OC class, you cannot define a function repeatedly to identify whether the function is repeated based on the method name, regardless of the receiving parameter. Different from java

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

// The same method getInfo:-(void) getInfo :( Animal) a;-(void) getInfo :( Person) p;

3. Common collections

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]);} // write it into the file. Whether atomically saves the file to the temporary file first. After the file is saved successfully, it is replaced with the original file. This is a security mechanism. [Array writeToFile: @ "/Users/zf/Desktop/1.txt" atomically: YES]; // read the file 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: an ordered set of elements in the memory of a whole block and arranged in order;

NSSet: unordered set, hashed storage.
Read developer. apple's explanation of 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.
When you search for an element, NSSet is more efficient than NSArray. Why? The principle is relatively simple: the storage and access of elements in NSSet are a hash process. For example, if you want to store element A, A hash algorithm can directly locate the location where A should be stored. Similarly, when you want to access, A hash process can locate the storage location of. For NSArray, if you want to know whether A is not in the array, You need to compare the elements one by one. Obviously, the efficiency is not high.
4. NSString
void stringTest(){    NSString *str = [NSString stringWithFormat:@"aa,bb,cc,"];    NSArray *array = [str componentsSeparatedByString:@","];    NSLog(@"len = %lu", [array count]);        NSLog(@"%@", [array componentsJoinedByString:@"-"]);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.