Created by Guo Chai April 04, 2015 20:35:30
A. Change the "Art and Literature Youth" to "213 youth".
Converts the integer 123 to the string "123".
Will the word "I love You" be the first word? Mother change Capital "I Love You"
Intercepts the string "|" in the string "20|http://www.baidu.com" and the back face, and outputs it.
NSString * str = [[NSString alloc] initwithstring:@ "literary Youth"]; NSString * str = @ "literary Youth"; NSLog (@ "%@", str); NSString * str2 = [str stringbyreplacingoccurrencesofstring:@ "literary" withstring:@ "123"]; NSLog (@ "%@", str2); ================================================= Nsinteger a = 122; NSString * STR3 = [NSString stringwithformat:@ "%ld", a]; NSLog (@ "%@", STR3); ================================================= NSString * STR4 = @ "I love You"; NSString * STR5 = [STR4 capitalizedstring]; The first letter of each word in the string is capitalized NSLog (@ "%@", STR5); ================================================= NSString * str6 = @ "20|http://www.baidu.com"; Nsarray * Arry = [STR6 componentsseparatedbystring:@ "|"]; For (NSString *s in Arry) { NSLog (@ "%@", s); }
Two. store int, float, double, BOOL, Nsrange, etc. in a variable array
Data Traversal Array
Example: Converting a scalar to nsnumber or nsvalue to store
NSNumber * Int_number = [NSNumber numberwithint:10]; Nsmutablearray * Mularr = [Nsmutablearray arraywithobjects:int_number, nil]; NSNumber * Double_number = [NSNumber numberwithdouble:23.5]; [Mularr Addobject:double_number]; NSNumber * Bool_number = [NSNumber numberwithbool:yes]; [Mularr Addobject:bool_number]; Nsrange range = {1,5};//Note This cannot be taken with the * number nsvalue * range_struct = [Nsvalue valuewithrange:range]; [Mularr addobject:range_struct]; NSLog (@ "%@", range_struct); for (int i = 0;i < [Mularr count];i + +) { NSLog (@ "%@", Mularr[i]); NSLog (@ "%@", [Mularr objectatindex:i]); } If you need to change back to the original type, you need to use Intvalue,doublevalue
Three. Implement simple address Book operations.
1, the definition of contact? human contacts. Instance variables: Name, gender, phone number, address, group name. Method: Initialize Method (name, phone number), display contact information?
2, define the variable array in main.m, manage the contact person. Can I add a new contact? Person Object, if the name or phone number is empty, the print add fails.
3. Get all the contacts under a group?
4, according to phone number search contact??
5. Get all female contacts?
6, according to the name delete contact?
7. Delete a group all contact?
8. Show all contacts in the Address Book?
9, choose to do: Define the AddressBook class, encapsulating the above functions.
===================================================================================
I'm defining a Nsmutablearray classification and then implementing a different block of code in it.
Method class:
-(void) Addcontact: (Contact *) contact;-(void) Getcontactsofanyoneofgroub: (NSString *) groub;-(void) Searchcontactwithphone: (NSString *) phone;-(void) Deletecontactwithname: (NSString *) name;-(void) Deletecontactofgroub: (NSString *) groub;-(void) showallcontacts;
Realize:
<span style= "FONT-SIZE:18PX;" >-(void) Addcontact: (Contact *) contact{if ([Contact.name isequaltostring:@ ""]==1) | | ([Contact.phone isequaltostring:@ ""]==1)] {NSLog (@ "Add failed!) "); } else{[self addobject:contact]; }}-(void) Getcontactsofanyoneofgroub: (NSString *) groub{for (contact *contact in self) {if ([contact]. Groub Compare:groub] = = 0) NSLog (@ "%@", contact); }}-(void) Searchcontactwithphone: (NSString *) phone{int temp = 0; for (*contact) {if ([contact.phone compare:phone] = = 0) {NSLog (@ "%@", contact) ; temp = 1; }} if (temp = = 0) {NSLog (@ "Do not has this contact"); }}-(void) Deletecontactwithname: (NSString *) name{for (int i = 0; i < [self count]; i + +) {if ([[Self[i] Nam E] compare:name] = = 0) {[self removeobject:self[i]]; Break }}}-(void) Deletecontactofgroub: (NSStRing *) groub{for (int i = 0; i < [self count]; i + +) {if ([[Self[i] Groub] compare:groub]==0) { [Self removeobject:self[i]]; I--; Key: Backtrack}}}-(void) showallcontacts{for (contact *contact in self) NSLog (@ "%@", contact);} </span>of course, don't forget:
-(NSString *) description{ nsstring * str = [[NSString alloc]initwithformat:@ "%@%@%@", _name,_phone,_groub]; return str;}
=======================================================================================
Four. Add initialization to NSString? Method Initwithdate:forformat:.
The first parameter is the NSDate object, and the first two parameters are the NSString object, the two parameters? is used to specify the format of the NSDateFormatter.
-(Instancetype) Initwithdate: (NSDate *) Date Forformat: (NSString *) str{ nsdateformatter * formatter = [[ NSDateFormatter alloc] init]; [Formatter setdateformat:str]; str = [Formatter stringfromdate:date]; return str;}
===================================== nsdate * date = [NSDate date]; NSLog (@ "DD"); NSString * str = [[NSString alloc] initwithdate:date forformat:@ "Yyyy-mm-dd HH:mm:ss"]; NSLog (@ "%@", str);//=====================================
Five. Add Convert method to Nsmutablearray to implement the array reverse
+ (void) Convert: (Nsmutablearray *) mularray{for (int i = 0,J = (int) [Mularray count]-1; i<j; i ++,j--) { [Mula Rray exchangeobjectatindex:i withobjectatindex:j]; } } -(void) Convert: (Nsmutablearray *) mularray{for (int i = 0,J = (int) [Mularray count]-1; i<j; i ++,j--) { [Mula Rray exchangeobjectatindex:i withobjectatindex:j]; }}
Using the class method and the object method to achieve, the difference is where you know ....
Six. Define the marriage agreement, woman is the agent, for boy to do housework to make money .....
@protocol marryprocotol <NSObject> @required-(void) Makemoney; @optional-(void) Cook; @end
Woman
@interface woman:nsobject<marryprocotol>
-(void) makemoney{ NSLog (@ "Woman--makemoney");} -(void) cook{ NSLog (@ "Woman--cook");}
Boy
Id<marryprocotol> _protocol;-(void) Setprotocol: (id<marryprocotol>) protocol;-(id<MarryProcotol >) protocol;-(void) playgame;-(void) watchfilm;
-(void) playgame{ NSLog (@ "Boy--playgame"); [_protocol Makemoney];} -(void) watchfilm{ NSLog (@ "Boy--watchfilm"); [_protocol Cook];} -(void) Setprotocol: (id<marryprocotol>) protocol{ _protocol = protocol;} -(id<marryprocotol>) protocol{ return _protocol;}
Below woman Mughal Boy does the housework to make money
Boy *boy =[[boy alloc] init]; Woman *woman = [[Woman alloc]init]; [Boy Setprotocol:woman]; [Boy PlayGame]; [Boy Watchfilm];
Seven. Definition? A block with a return value of bool with two nsstring parameters. Implementation: Sentencing
The broken strings are equal.
typedef BOOL (^block) (nsstring*,nsstring*);
Block block = ^ (NSString *str1,nsstring *str2) { if ([str1 isequaltostring:str2]) { return YES; } else return NO; }; if (Block (@ "SDS" @ "DDD")) { NSLog (@ "Equal"); } else NSLog (@ "! Equal ");
Eight. Definition? A block with a return value of Nsinteger, with two parameters? One is Nsarray, and the other is nsstring. Implementation: The array is judged to contain this string, if included, returns the subscript of the string, if not included, returns 1
__block int i = 0; Block2 Block2 = ^ (nsstring *str,nsarray * arr) {for (i = 0; i < [arr count]; i + +) { NSString * str2 = [arr obj ECTATINDEX:I]; if ([str isequaltostring:str2]) return (Nsinteger) i; } Return (Nsinteger)-1; }; Nsarray * array = [Nsarray arraywithobjects:@ "Guo", @ "Zai", nil]; NSLog (@ "%ld", Block2 (@ "Zai", Array));
Nine. Create an array that is initialized to @ "123", @ "21", @ "33", @ "69", @ "108",
@ "256". To use the block syntax, to sort the array of rows in. and output the content. Sorted results: 108 123 21 256 33 69
!
Reference: Sortedarraywithoptions:usingcomparator:
Nsarray * arr = [Nsarray arraywithobjects:@ "123", @ "+" @ "@", @ "," @ "108", @ "" ", nil]; Nsarray * arr2 = [arr sortedarraywithoptions:nssortstable usingcomparator:^nscomparisonresult (NSString *obj1, NSString *OBJ2) { if ((int) [obj1 compare:obj2]>0) return nsordereddescending; else if ([obj1 compare:obj2]==0) return nsorderedsame; else return nsorderedascending; }]; For (NSString * s in arr2) { NSLog (@ "%@", s); }
See you tomorrow!!!!!!!!!!!!!
OBJECTC----a few little exercises