IOS_OC_ Basic Syntax

Source: Internet
Author: User

1.YES and no

OBJECT-C provides a bool type, but this bool type is not the same as in C + +: everything in C + + that is not 0 value is true, and the value of 0 is false. But Object-c 1 is true and is defined by the macro as yes=1,0 false and defined by the macro 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 a class of OC, it is not allowed to define a function repeatedly, whether the function is repeated according to the method name, regardless of the receive parameter. Different from Java

pubic void GetInfo (Animal a);p ublic 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", @ "one", @ "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 into the file, atomically whether to save the file to a temporary file, after the successful save, and then replace with the original file, is a security mechanism.    [Array writetofile:@ "/users/zf/desktop/1.txt" atomically:yes];    Read 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:@ ""];    [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 an entire block of memory and arranged sequentially;

Nsset: Unordered collection, hash store.
Read Developer.apple's explanation of Nsset: You can use sets as a alternative to arrays when the order of elements isn ' t important and per Formance in testing whether an object was contained in the set is a consideration-while arrays be ordered, testing for MEM Bership is slower than with sets.
Searching for an element, Nsset is more efficient than nsarray. Why is it? The principle is simple: the storage and access of elements in Nsset is a hash process. For example, if you want to store element A, a hash algorithm can directly find the location where a should be stored, and also, when you want to access a, a hash process can find the location of a storage. For Nsarray, if you want to know if a is not in the array, you need an element comparison, obviously inefficient.
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.