Today I learned some basic classes in the foundation framework that are often used in objective-c, including NSNumber (numbers, characters, etc.), nsstring (strings), Nsarray (arrays), nsdictionary (dictionaries, In fact, it is also a collection Class) and Nsset (collection) and so on. These classes are contained in the Foundation.h class library, and each class declares some very useful methods that they can think of as some basic data types in OC, so their importance is self-evident. The following is a summary of how they are used:
1, NSNumber
There are some basic data types in C, such as int, float, char, and so on, and the NSNumber class provided in OC can convert these basic types of data and objects into each other using the declared method to facilitate specific operations.
1) Basic type ===〉 object, there are two methods can be implemented, namely, class method and instance method:
Intnumr=1;
floatnum2=4.5;
Blloisbool=no;
Charc= ' C ';
class method implementation
Nsnumber*intnumber=[nsnumber Numberwithint:num];
Nsnumber*floatnumber=[nsnumber Numberwithfloat:float];
Example method implementation
Nsnumber*isboolnumber = [[NSNumber alloc] initwithbool:isbool];
Nsnumber*cnumber = [[NSNumber alloc] initwithchar:c];
Print
NSLog (@ "intnumber:%@", intnumber);
NSLog (@ "floatnumber:%@" floatnumber);
NSLog (@ "Isboolnumber:%@", isboolnumber);
2) Object ===〉 Basic type
int d = [intnumber intvalue];
float f1 = [Floatnumber floatvalue];
char C1 = [Cnumber charvalue];
NSLog (@ "%d,%f,%c", D,F1,C1);
2, NSString
NSString is the OC-heavy string type, which defines the object as a string object, and the format of the string object is @ "string content". Its method is used as follows:
String creation
String constants
NSString *str = @ "good. ";
Variable of string
1) Empty string creation
NSString *STR2 = [[NSString alloc] init];
STR2 = @ "Test";
NSString *STR3 = [NSString string];
NSLog (@ "str =%@,str2=%@,str3=%@", STR,STR2,STR3);
2) quickly create a string
NSString *STR4 = [[NSString alloc]initwithstring:@ "Hello"];
NSSTRING*STR5 = [NSString stringwithstring:@ "World"];
NSLog (@ "STR4 =%@,str5=%@", STR4,STR5);
3) formatting to create a string
NSString *STR6 = [[NSString alloc]initwithformat:@ "%d_%d_%d_%d_%d_%@", 1,2,3,4,5,STR4];
NSLog (@ "str6=%@", STR6);
Determine if strings are equal
if ([STR4 ISEQUALTOSTRING:STR5]) {
NSLog (@ "string equal");
}else{
NSLog (@ "string not Equal");
}
NSLog (@ "%p,%p", STR4,STR5);
Determine if a string is the same object
if (str4== str5) {
NSLog (@ "is the same object.) ");
}
Basic data type è string
STR =[[nsstring alloc] initwithformat:@ "%d", 5];
String ===〉 base data type
NSSTRING*STR7 = [NSString stringwithformat:@ "%d", 56];
int num7= [STR7 intvalue]; string-To-integer
NSLog (@ "num7+1=%d", num7+1);
string conversion è array
Nsarray*array = [Str6 componentsseparatedbystring:@ "_"];
NSLog (@ "%@", array);
String interception
NSLog (@ "Substringto 5:%@", [STR6 substringtoindex:5]);
NSLog (@ "Substringfrom 5:%@", [STR6 substringfromindex:5]);
To intercept a string of a range
Nsrangerang;
Rang.length= 4;
Rang.location= 2; When intercepted, contains the starting position
NSLog (@ "substring:%@", [STR6 Substringwithrange:rang]);
String lookup (Find substring)
nsstring*str8=@ "Hello01.txt";
Find, return range
NSRangerang2 = [Str8 rangeofstring:@ ""];
if (rang2.location!= nsnotfound) {
NSLog (@ "substr location =%ld, length=%ld", rang2.location,rang2.length);
}else{
NSLog (@ "nsnotfound!!");
}
Variable-length strings
NSMUTABLESTRING*MUTABLESTR = [nsmutablestring stringwithstring:@ "Love Rice"];
Dynamically inserted content
[mutablestrinsertstring:@ "Mouse" atindex:0];
NSLog (@ "mubablestr:%@", mutablestr);
3, Nsarray
In OC, the Nsarray class is equivalent to the array type in C, which is used to store an ordered list of objects, you can put objects of any class in the Nsarray object, Nsarray creates an array of immutable objects, so there is a supplemental class Nsmutablearray. This allows us to arbitrarily add and remove objects from the array. Their basic usage is as follows:
Define an array and initialize
Nsarray *array1 = [nsarrayarraywithobject:@ "one"];
Nsarray *array2 = [Nsarray arraywithobjects:@ "one", @ "one", @ "three", @ "four", nil];
Nsarray *array3 = [Nsarrayarraywitharray:array2];
NSLog (@ "array1 =%@, array2 =%@, Array3 =%@", array1,array2,array3);
Access to arrays
Request length
int len = [array2 count];
accessing elements
NSString *arrayobject = [Array3 objectatindex:3];
Concatenate array elements into a single string
NSString *newstr = [array2componentsjoinedbystring:@ "_"];
NSLog (@ "Array2 length:%d,index3=%@,joinstr =%@", len,arrayobject,newstr);
Use of variable groups
Nsmutablearray *mutablearray = [nsmutablearrayarraywithobjects:@ "One", nil];
----adding elements
[Mutablearray addobject:@ ""];
[Mutablearray addobject:@ "three"];
[Mutablearray addobject:@ "four"];
-------------Add an array
[Mutablearray Addobjectsfromarray:array2];
----Calculation length
int length = [Mutablearray count];
NSLog (@ "Mutablearray length=%d,countent:%@", Length,mutablearray);
----Remove the last
[Mutablearray Removelastobject];
----Remove the specified data
[Mutablearray removeobjectatindex:0];
length = [Mutablearray count];
NSLog (@ "***mutablearraylength=%d,countent:%@", Length,mutablearray);
How arrays are traversed: an efficient way of traditional methods
-----Traditional Way
for (int i=length-1; i>=0; i--) {
NSLog (@ "%d =%@", I,[mutablearray objectatindex:i]);
}
-----efficient way, it's important to note that the high-efficiency approach is a new feature that starts with objective-c2.0, and that if you or/your users need to run on a tiger (MAC OS X10.4) system, you can't use this new syntax, which is really/really bad.
For (NSString *str in Mutab learray) {
NSLog (@ "obj =%@", str);
}
4, Nsdictionary
The dictionary type of OC, which is the set of keywords and their definitions. Nsdictionary stores a numeric value (which can be any type of object) under a given keyword (usually a nsstring string). Then you can use this keyword to find the corresponding value.
-----initialization
NSNumber *numobj = [nsnumbernumberwithint:100];
Initialize a set of arrays
Value key
Nsdictionary *dic1 = [nsdictionarydictionarywithobject:numobj forkey:@ "Key1"];
Initializing multiple sets of data
nsdictionary*dic2=[nsdictionarydictionarywithobjectsandkeys:@ "Hello", @ "Key2", @ "World", @ "Key3", @ "csdn", @ "Key4", NIL];
Initialize another dictionary with a dictionary
Nsdictionary *dic3 = [Nsdictionarydictionarywithdictionary:dic2];
Print output
NSLog (@ "Dic1:%@,dic2:%@, Dic3:%@", DIC1,DIC2,DIC3);
------Get values
Get length
int len = [Dic2 count];
NSLog (@ "Dic2 length =%d", Len);
Gets the value corresponding to key from key
NSLog (@ "Key3 value =%@", [dic2objectforkey:@ "Key3"]);
Can get all the keys
Nsarray *allkeys = [dic3 AllKeys];
NSLog (@ "Nsarray allkey =%@", AllKeys);
You can get all the values
Nsarray *allvalues = [dic3 allvalues];
NSLog (@ "Nsarray allvalues =%@", allvalues);
-----variable Dictionaries
-----initialization
nsmutabledictionary*dic4=[nsmutabledictionarydictionarywithobjectsandkeys:@ "One", @ "Key4", @ "one", @ "Key5", nil];
NSLog (@ "DIC4:%@", DIC4);
Define an empty dictionary
Nsmutabledictionary *dic5 =[nsmutabledictionary Dictionary];
Add a whole dictionary dic2 to the DIC4
[Dic4 Addentriesfromdictionary:dic2];
NSLog (@ "Addentriesfromdictionary dic2:%@", DIC4);
Add an Element
[dic4setvalue:@ "three" forkey:@ "Key6"];
NSLog (@ "Dic4 setValue:%@", DIC4);
Get value based on key
NSLog (@ "Key6 =%@", [dic4objectforkey:@ "Key6"]);
------The traversal of a dictionary
1) General traversal
Nsarray *KEYS4 = [Dic4 AllKeys];
for (Inti=0;i<[dic4 count];i++) {
NSLog (@ "Dic4 key =%@,value=%@", [Keys4 objectatindex:i],[dic4 objectforkey:[keys4objectatindex:i]]);
}
2) Efficient for
For (NSString *key in Dic4) {
NSLog (@ "Dic4 key =%@, Value =%@", Key,[dic4 Objectforkey:key]);
}
3) using enumerations to traverse
Nsenumerator *enum1 = [Dic4 keyenumerator];
Gets the key, or offsets if it is not empty
ID key = [enum1 nextobject];
while (key) {
NSLog (@ "key=%@, value =%@", Key,[dic4 Objectforkey:key]);
Key =[enum1 Nextobject];
}
5, Nsset
The keyword for a collection object is Nsset and Nsmutableset, which is an immutable collection, which is a mutable collection. Nsset objects are similar to Nsarray object usages, but Nsset object access is slower and Nsarray objects are stored more quickly. Use the following:
-----definition, initialization
Nsset *set = [[Nsset alloc]initwithobjects:@ "one", @ "one", @ "one", nil];
Define nsset with an array;
Nsarray *arrayset = [nsarrayarraywithobjects:@ "1", @ "2", @ "3", nil];
Nsset *set2 = [Nsset setwitharray:arrayset];
NSLog (@ "Set1 =%@,set2 =%@", Set,set2);
Access mode
-----Get length
int len = [Set2 count];
NSString *s = [Set2 anyobject];
NSLog (@ "Set2 length =%d,obj =%@", len,s);