OC Miscellaneous, 1214

Source: Internet
Author: User

1,os shortcut keys

Command (Long Press) + TAB, Toggle program window.

Command (Long Press) + Space, toggle IME.

2, the scope of the pre-processing instruction is from his appearance to the end of the file, C language provides preprocessing directives mainly include: macro definition, file contains, conditional compilation , #include "", the system will first in the source program in the current directory, I can't find it. Look in the path path of the operating system, and finally go to the directory where the C-language function header file is located. #include指令允许嵌套包含, for example, A.H contains b.h,b.h containing c.h, but does not allow recursive inclusion, such as A.H contains B.h,b.h. Use #ifndef _dsd_h_, #define _DSD_H_, to prevent content from being duplicated. #import可以自动防止同一个文件被导入多次.

3,weak and Strong

3.1. (weak and strong) different: When an object no longer has a strong type pointer to it, it will be released, even if the object has a _weak type pointer to it;3.2. Once the last pointer to the strong type of the object is left, the object will be freed, and if there is a weak pointer pointing to the object, all remaining weak pointers will be cleared out. as long as the last strong pointer no longer points to the object, the object is freed and all weak pointers are cleared.  Strong in OC is equivalent to the Retain attribute, and weak is equivalent to assign. There is only one situation where you need to use weak (the default is strong), just to avoid retain cycles (that is, the parent class has a subclass {parent class retain the subclass}, and the child class calls the parent class {subclass and retain the parent class}, which cannot be release)

  The weak pointer is used primarily for "parent-child" relationships, where the father has a strong pointer to the son, so the father is the owner of the son, but in order to prevent the cycle of ownership, the son needs to use the weak pointer pointing to his father. A typical example is the delegate mode, where your viewcontroller has a uitableview through the strong pointer (Self.view), UITableView DataSource and delegate are weak pointers, Point to your Viewcontroller

Strong and weak pointers are used with caution.

The following code is problematic:

    1. __weak nsstring *str = [[NSString alloc] initwithformat:@"1234"];
    2. NSLog (@"%@", str); //Print Out is "(null)"

STR is a weak pointer, so the NSString object has no owner and is released immediately after creation. Xcode will also give a warning ("warning:assigning retained object to weak variable; Object would be released after assignment ")

4,self:

Who calls Self,self is pointing to WHO
The Self keyword must be used in the method of the class

5, polymorphic, polymorphic dependent on inheritance, the object of the child class is saved with a pointer to the parent class. The method that passes in which object is called which object.

6, Property Builder

@property Auto-generated attribute declarations
@synthesize Auto-generated property implementation, Xcode4.3 will not need to write 7, the array of componentseparatebystring: To split the Nsarray, with componentsjoinedbystring to merge , e.g. NSString *ss = @ "Opp:ack:dwdew:oooo";
Nsarray *ass = [ss componentsseparatedbystring:@ ":"];
For (NSString *tt in) {
NSLog (@ "==%@\n", TT);
}

NSString *hebing = [componentsjoinedbystring:@ ":"];
NSLog (@ "----%@", hebing);
/*
2014-12-16 09:10:07.101 basicoc[837:12532] ==opp
2014-12-16 09:10:07.102 basicoc[837:12532] ==ack
2014-12-16 09:10:07.102 basicoc[837:12532] ==dwdew
2014-12-16 09:10:07.103 basicoc[837:12532] ==oooo
2014-12-16 09:10:07.103 basicoc[837:12532]----opp:ack:dwdew:oooo
*/8, writes the array to the file, NSString, Nsdictionary,nsdata also has similar function. Nsarray is as follows://Nsarray *test = @[@ "1RRW", @ "2RRW", @ "R3RW", @ "rr4w", @ "R5RW",];
[Test writetofile:@ "/users/god_love_yao/desktop/test/basicoc/basicoc/deren.txt" atomically:1];

Nsarray *test2 = [Nsarray arraywithcontentsoffile:@ "/users/god_love_yao/desktop/test/basicoc/basicoc/deren.txt"];
For (NSString *tt in test2) {
NSLog (@ "===%@\n", TT);
}
/*
2014-12-16 09:50:26.681 basicoc[1578:27734] ===1rrw
2014-12-16 09:50:26.682 basicoc[1578:27734] ===2rrw
2014-12-16 09:50:26.682 basicoc[1578:27734] ===r3rw
2014-12-16 09:50:26.682 basicoc[1578:27734] ===rr4w
2014-12-16 09:50:26.682 basicoc[1578:27734] ===r5rw
*/9 copy and mutablecopy Copy the returned objects are immutable and mutablecopy are mutable. (regardless of the kind of pointer to receive). <1> copied objects are non-container class objects of the system: NSString, NSNumber, and so on. when the copied object is immutable, the object used by copy is the same as the address of the heap area (shallow copy). The address in the heap area obtained using mutablecopy is not the same (deep copy)(A new space is created), for example nsstring, nsstring *s1 = @ "Deren"; The object being copied is immutable

NSString *S2 = [S1 copy];    Here if the nsmutablestring to receive the object is still immutable, if modified ("Deren") the program will collapse.  nsmutablestring *S3 = [S1 mutablecopy]; It seems that there is no big relationship here to receive nsstring.

NSLog (@ "&s1==%p,&s2==%p,&s3==%p\n", &AMP;S1,&AMP;S2,&AMP;S3); The address of the stack area.
NSLog (@ "s1==%p,s2==%p,s3==%p", S1,S2,S3); The address of the heap area.

/*
2014-12-16 10:49:30.518 basicoc[2217:42363] &s1==0x7fff55947a98,&s2==0x7fff55947a90,&s3== 0x7fff55947a88
2014-12-16 10:49:30.519 basicoc[2217:42363] S1==0x10a2b8068,s2==0x10a2b8068,s3==0x7f8b80c6be10
*/ when the object being copied is mutable, the objects returned using copy and mutablecopy are not the same in the heap (deep copy), for example:   

nsmutablestring *s1 = [nsmutablestring stringwithstring:@ "DW"]; The objects being copied are mutable

NSString *S2 = [S1 copy];
nsmutablestring *S21 = [S1 copy];

NSString *s31 = [S1 mutablecopy];
nsmutablestring *S3 = [S1 mutablecopy];

NSLog (@ "&s1==%p,&s2==%p,&s21==%p,&s3==%p,&s31==%p,\n", &S1,&S2,&S21,&S3, &S31);
NSLog (@ "s1==%p,s2==%p,s21==%p,s3==%p,s31==%p,\n", s1,s2,s21,s3,s31);
/*
2014-12-16 14:32:55.062 basicoc[3119:68992] &s1==0x7fff54f75a98,&s2==0x7fff54f75a90,&s21== 0X7FFF54F75A88,&S3==0X7FFF54F75A78,&S31==0X7FFF54F75A80,
2014-12-16 14:32:55.063 basicoc[3119:68992] s1==0x7fe4b1e97750,s2==0x7fe4b1e978a0,s21==0x7fe4b1e97570,s3== 0x7fe4b1e993d0,s31==0x7fe4b1e97640,
*/

<2> replicated objects are System container class objects: Nsarray, Nsdictionary, and so on. For the container class itself, the conclusions discussed above are also applicable

  when the copied object is immutable, the object used by copy is the same as the address of the heap area (shallow copy). The address in the heap area obtained using mutablecopy is not the same (deep copy)(a new space is created),

  When the object being copied is mutable, the objects returned using copy and mutablecopy are not the same in the heap (deep copy), for example:

NSString *ss1 = @ "daa123";//NSString change to nsmutablestring as well

NSString *ss2 = @ "Frgre";//NSString change to nsmutablestring as well

Nsarray *array1 = [Nsarray arraywithobjects:ss1,ss2, Nil];
Nsmutablearray *marray1 = [Nsmutablearray arraywithobjects:ss1,ss2, Nil];

Nsarray *carray1 = [array1 copy];//= = =
Nsarray *carray2 = [array1 mutablecopy];//x = =
Nsmutablearray *carray3 = [array1 copy];//= = =
Nsmutablearray *carray4 = [array1 mutablecopy];//x = =

Nsarray *carray1 = [marray1 copy];//x = =
Nsarray *carray2 = [marray1 mutablecopy];//x = =
Nsmutablearray *carray3 = [marray1 copy];//x = =
Nsmutablearray *carray4 = [marray1 mutablecopy];//x = =
(The following note "x = =" means that the array pointer returned by the copy is not the same as Object 1 in the array, object 2) you can see that the copy of the array is copied, however it is (depending on whether the object being copied is mutable) The pointer is copied, but the objects contained in the array are not copied. A, producing a mutable copy of an object does not require that the copied object itself be mutable. You can also create immutable copies of Mutable objects. B,When a copy of the array is generated, the hold count of each element in the array is automatically increased by 1 through the copy operation. Therefore, it is necessary [release]; free its memory10, archive and archive: <1> The archive of a single object, the process is as follows: NSString *homedirectory = Nshomedirectory ();// E7BABD79-EA1A-491F-A700-9E8409845D1E, here two times the last level of the directory is not the same, so the following delete the last level of the directory,
194008ed-75da-428e-8e8d-3d596421b297
NSString *last = [HomeDirectory stringbydeletinglastpathcomponent]; Delete last level directory


NSString *filepath = [last stringbyappendingstring:@ "/aaaa.h"];
Nsarray *array = @[@ "Hello" @ "world" @ "!", @ "-----"];
if ([Nskeyedarchiver archiverootobject:array Tofile:filepath]) {
NSLog (@ "filepath==%@", FilePath);
//    };

Nsarray *getarray = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath];
NSLog (@ "---==%@", getarray); <2> archiving of multiple objects, the process is as follows: NSString *homedirectory = Nshomedirectory ();
NSString *last = [HomeDirectory stringbydeletinglastpathcomponent];
NSString *filepath = [last stringbyappendingstring:@ "/aaaa.h"];

Nsmutabledata *archidata = [[Nsmutabledata alloc]init];
Nskeyedarchiver *archi = [[Nskeyedarchiver alloc]initforwritingwithmutabledata:archidata];
[Archi encodeobject:@ "Hello" forkey:@ "Hello"];
[Archi encodeint:123 forkey:@ "123"];
[Archi encodeobject:@ "nice good" forkey:@ "good"];
[Archi finishencoding];
if ([Archidata writetofile:filepath atomically:1])
//    {
NSLog (@ "------");
//    };

Nsmutabledata *unarchidata = [[Nsmutabledata Alloc]initwithcontentsoffile:filepath];
Nskeyedunarchiver *unarchi = [[Nskeyedunarchiver alloc]initforreadingwithdata:unarchidata];
NSLog (@ "---=%@\n---=%d\n---=%@\n", [Unarchi decodeobjectforkey:@ "Hello"],[unarchi decodeintforkey:@ "123"],[ Unarchi decodeobjectforkey:@ "good"]);

/*
2014-12-18 11:24:56.746 basicoc[1126:25658]---=hello
---=123
---=nice good
*/

OC Miscellaneous, 1214

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.