Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common functions of API library in OC

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "what are the common functions of the API library in OC", the content is simple and clear, and I hope it can help you solve your doubts, so let me lead you to study and learn about the common functions of the API library in OC.

NSMutableArray variable array

/ / initWithCapacity: create an array

/ /-(instancetype) initWithCapacity: (NSUInteger) numItems

NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:1]; / / 1 means to allocate an object-sized memory to the array at the beginning. When the array already has an object, when you put another object, the array will automatically add 1 object-sized memory (n represents the number of additional memory). The same object can be stored in an array multiple times

NSLog (@ "% @", array)

AddObject: add elements (objects) to an array

-(void) addObject: (id) anObject

NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:1]

/ / 1 means that at the beginning, an object-sized memory is allocated to the array. When the array already has an object and another object is placed, the array will automatically add 1 object-sized memory (n represents the number of additional memory). The same object can be stored in an array multiple times

Person * p1 = [[Person alloc] initWithName:@ "Zhang 1" sex:@ "male" age:71]

Person * p2 = [[Person alloc] initWithName:@ "Li 2" sex:@ "male" age:22]

Person * p3 = [[Person alloc] initWithName:@ "King 3" sex:@ "female" age:63]

[array addObject:p1]; [array addObject:p2]; [array addObject:p1]

NSLog (@ "% @", array)

/ / insertObject:atIndex:

[array insertObject:p2 atIndex:2]

NSLog (@ "insert p2% @" in subscript 2, array)

/ / removeLastObject

[array removeLastObject]

NSLog (@ "remove last element% @", array)

/ / removeObject:

[array removeObject:p2]

NSLog (@ "remove p2 element @", array)

/ / removeObject:inRange:

[array addObject:p2]; [array addObject:p3]

[array addObject:p1]

NSLog (@ "after adding% @", array)

[array removeObject:p1 inRange:NSMakeRange (0,4)]

NSLog (@ "Delete objects that appear in a given range% @", array)

/ / removeObjectAtIndex:

[array removeObjectAtIndex:1]

NSLog (@ "Delete the given subscript element% @", array)

/ / replaceObjectAtIndex:withObject:

[array replaceObjectAtIndex:0 withObject:p3]

NSLog (@ "after replacement% @", array)

/ / exchangeObjectAtIndex:withObjectAtIndex:

[array addObject:p1]; [array addObject:p2]

[array exchangeObjectAtIndex:1 withObjectAtIndex:2]

NSLog (@ "Exchange subscript 1 and subscript 2 elements% @", array)

Print the results:

2015-01-15 22 53 OC 04.514 January 15 [3452 303] (

"\ U5f201\ U7537 71"

"\ U674e2\ U7537 22"

"\ U5f201\ U7537 71"

)

2015-01-15 22 53 OC 04.516 January 15 insert p2 (

"\ U5f201\ U7537 71"

"\ U674e2\ U7537 22"

"\ U674e2\ U7537 22"

"\ U5f201\ U7537 71"

)

2015-01-15 22 53 04.517 January 15 [3452 OC] delete the last element (

"\ U5f201\ U7537 71"

"\ U674e2\ U7537 22"

"\ U674e2\ U7537 22"

)

2015-01-15 22 53 OC January 15 [3452 303] delete the p2 element (

"\ U5f201\ U7537 71"

)

2015-01-15 22 53 OC 04.518 January 15 [3452 303] after addition

"\ U5f201\ U7537 71"

"\ U674e2\ U7537 22"

"\ U738b3\ U5973 63"

"\ U5f201\ U7537 71"

)

2015-01-15 22 53 OC 04.556 January 15 [3452 303] Delete objects that appear within a given range (

"\ U674e2\ U7537 22"

"\ U738b3\ U5973 63"

)

2015-01-15 22 53 OC 04.556 January 15 [3452 303] Delete the given subscript element (

"\ U674e2\ U7537 22"

)

2015-01-15 22 53 OC 04.557 January 15 after replacement (

"\ U738b3\ U5973 63"

)

2015-01-15 22 OC 53 OC January 15 exchange subscript 1 and subscript 2 elements (

"\ U738b3\ U5973 63"

"\ U674e2\ U7537 22"

"\ U5f201\ U7537 71"

)

Person * p1 = [[Person alloc] initWithName:@ "Zhang 1" sex:@ "male" age:71]

Person * p2 = [[Person alloc] initWithName:@ "Li 2" sex:@ "male" age:22]

Person * p3 = [[Person alloc] initWithName:@ "King 3" sex:@ "female" age:63]

NSArray * array = [NSArray arrayWithObjects:p1,p2,p3,@ "1111", @ "222", nil]

NSLog (@ "% @", array)

For (int I = 0; I < [array count]; iTunes +) {

Id obj = [array objectAtIndex:i]

/ / check whether the object is an object of a class

/ / isKindOfClass

If ([obj isKindOfClass: [NSString class]]) {

NSString * newstr = [obj stringByAppendingString:@ "Blue Gull"]

NSLog (@ "% @", newstr)

}

}

2015-01-15 22 5715 53.527 OC January 15 [3466 303] (

"\ U5f201\ U7537 71"

"\ U674e2\ U7537 22"

"\ U738b3\ U5973 63"

1111

two hundred and twenty two

)

2015-01-15 22 57V 53.529 OC Jan 15 [3466 303] 1111 Blue Gull

2015-01-15 22 5715 53.530 OC January 15 [3466 303] 222Blue Gull

/ / * NSNumber*

/ / the NSNumber numerical class converts the basic numerical type to the object, or the numerical object to the basic type.

/ / intValue converts objects to numeric values.

/ / intValue and NSNumber numberWithInt: it's a couple

Int n = 5

NSNumber * N1 = [NSNumber numberWithInt:n]

NSArray * array = [NSArray arrayWithObjects:n1, [NSNumber numberWithInt:10], nil]

NSLog (@ "% @", array)

Int result = [[array firstObject] intValue] + [[array lastObject] intValue]

NSLog (@ "result =% d", result)

Print the results:

2015-01-15 22 5936.037 OC Jan 15 [3474 303] (

five,

ten

)

2015-01-15 22 5936.039 OC January 15 [3474 Vol 303] result = 15

/ / NSDictionary unmodifiable dictionary

/ / 1. Key value pair 2. Dictionary disorder 3. Deposit object 4. The corresponding value 5. Key is unique by key index

/ / initWithObjectsAndKeys:

NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:@ "Zhang San", @ "name", @ "male", @ "sex", @ "25", @ "age", nil]

NSLog (@ "% @", dic)

2015-01-15 2300 OC 33.258 January 15 [3484 Swiss 303] {

Name = "\ U5f20\ U4e09"

Sex = "\ U7537"

"\ U5e74\ U9f84" = 25

}

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@ "zhangsan", @ "name", @ "M", @ "sex", @ "30", @ "age", nil]

NSLog (@ "% @", dic)

/ / dictionaryWithObjects:forKeys: class method, which corresponds to the value and key respectively, and returns a dictionary

/ / + (instancetype) dictionaryWithObjects: (NSArray *) objectsforKeys: (NSArray *) keys

NSArray * arrayValue = [NSArray arrayWithObjects:@ "zhangsan", @ "30", @ "W", nil]

NSArray * keyArray = [NSArray arrayWithObjects:@ "name", @ "age", @ "sex", nil]

NSDictionary * dic1 = [NSDictionary dictionaryWithObjects:arrayValue forKeys:keyArray]

NSLog (@ "% @", dic1)

/ / count / / get the number of key-value pairs

NSLog (@ "% ld", [dic1 count])

/ / allKeys gets all the keys

NSLog (@ "% @", [dic1 allKeys])

/ / allValues gets all the values

NSLog (@ "% @", [dic1 allValues])

Print the results:

2015-01-15 23 01 OC 17.213 January 15 [3492 303] {

Age = 30

Name = zhangsan

Sex = M

}

January 15-2015-01-15 23. 01 OC 17. 216 January 15 [3492] {

Age = 30

Name = zhangsan

Sex = W

}

2015-01-15 23 01 OC 17.216 Jan 15 [3492 Vol 303] 3

2015-01-15 23 01 OC 17.217 January 15 (

Name

Age

Sex

)

2015-01-15 23 01 OC 17.217 January 15 (

Zhangsan

thirty,

W

)

/ / * * objectForKey: important * /

/ / objectForKey: index data according to Key

/ /-(id) objectForKey: (id) aKey

NSArray * arrayValue = [NSArray arrayWithObjects:@ "zhangsan", @ "30", @ "W", nil]

NSArray * keyArray = [NSArray arrayWithObjects:@ "name", @ "age", @ "sex", nil]

NSDictionary * dic1 = [NSDictionary dictionaryWithObjects:arrayValue forKeys:keyArray]

NSString * str = [dic1 objectForKey:@ "name"]

NSLog (@ "% @", str)

/ / iterate through the dictionary and take out all the values of the dictionary

For (int I = 0; I < [dic1 count]; iTunes +) {

NSString * key = [[dic1 allKeys] objectAtIndex:i]

NSString * value = [dic1 objectForKey:key]

NSLog (@ "traversal 1% @", value)

}

/ / traversing 2

NSArray * array = [dic1 allValues]

For (int I = 0; I < [dic1 count]; iTunes +) {

NSString * str = array [I]

NSLog (@ "traversal 2% @", str)

}

2015-01-15 23 05 OC 29.028 Jan 15 [3524 303] zhangsan

2015-01-15 23 05V 29.030 OC January 15th [3524v 303] traverse 1 zhangsan

2015-01-15 23 05 OC 29.031 Jan 15 [3524 Vol 303] traversing 1 30

2015-01-15 23 05V 29.031 OC January 15th [3524v 303] traverse 1 W

2015-01-15 23 05 OC 29.032 Jan 15 [3524 303] traversing 2 zhangsan

2015-01-15 23 05 OC 29.032 Jan 15 [3524 Vol 303] traversing 2 30

2015-01-15 23 05V 29.032 OC Jan 15 [3524 Vol 303] traverse 2 W

/ / * * NSMutableDictionary*

/ / NSMutableDictionary

/ / dictionaryWithCapacity: create a dictionary that can modify the content

/ / one Key corresponds to one value

/ / A value can correspond to multiple key

NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:1]

NSLog (@ "dic =% @", dic)

/ / setObject:forKey: add key-value pairs to the dictionary

/ /-(void) setObject: (id) anObjectforKey: (id) aKey

[dic setObject:@ "zhangsan" forKey:@ "name"]

[dic setObject:@ "zhangsi" forKey:@ "name"]; / / if the key is the same, the content of the key will be replaced

[dic setObject:@ "20" forKey:@ "value1"]

[dic setObject:@ "20" forKey:@ "value2"]

NSLog (@ "% @", dic)

/ / removeObjectForKey:

[dic removeObjectForKey:@ "value2"]

NSLog (@ "% @", dic)

/ / [dic removeAllObjects]

/ / NSLog (@ "% @", dic)

NSArray * array = [NSArray arrayWithObjects:@ "value1", @ "name", nil]

[dic removeObjectsForKeys:array]; / / remove values corresponding to multiple keys

NSLog (@ "% @", dic)

2015-01-15 23 08 OC 49.741 January 15 [3538 Swiss 303] dic = {

}

2015-01-15 23: 08 OC 49.743 January 15 [3538: 303] {

Name = zhangsi

Value1 = 20

Value2 = 20

}

2015-01-15 23 08 OC 49.744 January 15 [3538 Swiss 303] {

Name = zhangsi

Value1 = 20

}

2015-01-15 23 08 OC 49.744 January 15 [3538 Swiss 303] {

}

/ / * two-dimensional array of OC * / /

NSArray * firstArray = [NSArray arrayWithObjects:@ "111l", @ "222", @" 333", @ "444", nil]

NSArray * secondArray = [NSArray arrayWithObjects:@ "AAA", @ "BBB", @ "CCC", nil]

NSArray * thirdArray = [NSArray arrayWithObjects:@ "name", @ "sex", @ "age", nil]

NSArray * bigArray = [NSArray arrayWithObjects:firstArray, secondArray,thirdArray, nil]

NSLog (@ "% @", bigArray)

/ / 1 traversal array (two-layer loop, array in the array)

For (int I = 0; I < [bigArray count]; iTunes +) {

For (int j = 0; j < [[bigArray objectAtIndex:i] count]; jacks +) {

Id a = [[bigArray objectAtIndex:i] objectAtIndex:j]

NSLog (@ "result of traversal array:% @", a)

}

}

2015-01-15 23 09 OC 37.710 January 15 [3546 Jan 303] (

(

one hundred and eleven,

two hundred and twenty two,

three hundred and thirty three,

four hundred and forty four

),

(

AAA

BBB

CCC

),

(

Name

Sex

Age

)

)

2015-01-15 23 09V 37.713 OC January 15 [3546 Vol 303] ergodic array result: 111l

2015-01-15 23 09V 37.713 OC January 15 [3546 Vol 303] ergodic array result: 222nd

2015-01-15 23 09 OC 37.713 January 15 [3546 Vol 303] ergodic array result: 333

2015-01-15 23 09 OC 37.714 January 15 [3546 Vol 303] ergodic array result: 444

2015-01-15 23 09 OC 37.714 January 15 [3546 Vol 303] ergodic array result: AAA

2015-01-15 23 09 OC 37.715 January 15 [3546 Vol 303] ergodic array result: BBB

2015-01-15 23 09 OC 37.715 January 15 [3546 Vol 303] ergodic array result: CCC

2015-01-15 23 09 OC 37.715 January 15 [3546 Vol 303] ergodic array result: name

2015-01-15 23 09 OC 37.716 January 15 [3546 Vol 303] ergodic array result: sex

2015-01-15 23 09 OC 37.716 January 15 [3546 Vol 303] ergodic array result: age

/ / 2 Dictionary in the array

NSDictionary * stuDic1 = [NSDictionary dictionaryWithObjectsAndKeys:@ "zhangsan", @ "name", @ "M", @ "sex", @ "30", @ "age", nil]

NSDictionary * stuDic2 = [NSDictionary dictionaryWithObjectsAndKeys:@ "lisi", @ "name", @ "M", @ "sex", @ "25", @ "age", nil]

NSArray * array = [NSArray arrayWithObjects:stuDic1, stuDic2, nil]

NSLog (@ "% @", array)

For (int I = 0; I < [array count]; iTunes +) {

For (int j = 0; j < [[array objectAtIndex:i] count]; jacks +) {

Id result = [array objectAtIndex:i] allValues] objectAtIndex:j]

NSLog (@ "after traversal:% @", result)

}

}

2015-01-15 23 OC 10! 27.425 January 15 [3557: 303]

{

Age = 30

Name = zhangsan

Sex = M

}

{

Age = 25

Name = lisi

Sex = M

}

)

2015-01-15 23 10 OC 27.427 Jan 15 [3557 Suzhou 303] after traversing: zhangsan

2015-01-15 23: 10 OC 27.427 January 15 [3557: 303] after traversing: M

2015-01-15 23 OC 10 Jan 27.428 Jan 15 [3557 Suzhou 303] after traversing: 30

2015-01-15 23 10 OC 27.428 Jan 15 [3557 303] after traversing: lisi

2015-01-15 23: 10 OC 27.429 January 15 [3557: 303] after traversing: M

2015-01-15 23: 10 OC 27.429 January 15 [3557: 303] after traversing: 25

Put an array in the dictionary

NSArray * firstArray = [NSArray arrayWithObjects:@ "111l", @ "222", @" 333", @ "444", nil]

NSArray * secondArray = [NSArray arrayWithObjects:@ "AAA", @ "BBB", @ "CCC", nil]

NSArray * thirdArray = [NSArray arrayWithObjects:@ "name", @ "sex", @ "age", nil]

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:firstArray, @ "first", secondArray, @ "second", thirdArray, @ "third", nil]

NSLog (@ "% @", dic)

For (int I = 0; I < [dic count]; iTunes +) {/ / outer loop traversal dictionary

For (int j = 0; j < [dic allValues] objectAtIndex:i] count]) {/ / inner loop traverses the decimal array

Id a = [dic allValues] objectAtIndex:i] objectAtIndex:j]; / / fetch the contents of the array

NSLog (@ "after traversal% @", a)

}

}

2015-01-15 23 1120.271 OC January 15 [356515] {

First = (

one hundred and eleven,

two hundred and twenty two,

three hundred and thirty three,

four hundred and forty four

);

Second = (

AAA

BBB

CCC

);

Third = (

Name

Sex

Age

);

}

2015-01-15 23 1120.274 OC January 15 [356515] traversal Hou 111

2015-01-15 23 1120.274 OC January 15 [356515] traversal

2015-01-15 23 OC 1120.275 January 15 [356515] traversal

2015-01-15 23 1120.275 OC January 15 [356515] traversal

2015-01-15 23 1120.275 OC January 15 [3565 303] after AAA

2015-01-15 23 OC 1120.276 BBB after traversing

2015-01-15 23 OC 1120.276 CCC after traversing

2015-01-15 23 1120.277 OC January 15 [3565 303] after name

2015-01-15 23 1120.277 OC January 15 [3565 303] after sex

2015-01-15 23 1120.277 OC January 15 [3565 303] after age

/ / 4 the canon in the dictionary

NSDictionary * stuDic1 = [NSDictionary dictionaryWithObjectsAndKeys:@ "zhangsan", @ "name", @ "M", @ "sex", @ "30", @ "age", nil]

NSDictionary * stuDic2 = [NSDictionary dictionaryWithObjectsAndKeys:@ "lisi", @ "name", @ "M", @ "sex", @ "30", @ "age", nil]

NSDictionary * bigDic = [NSDictionary dictionaryWithObjectsAndKeys:stuDic1,@ "first", stuDic2, @ "second", nil]

NSLog (@ "% @", bigDic)

For (int I = 0; I < [bigDic count]; iTunes +) {

/ / the outer loop controls the logarithm of the key value of the large dictionary.

For (int j = 0; j < [bigDic allValues] objectAtIndex:i] count]; jacks +) {

/ / the inner loop controls the logarithm of the key values of the small dictionary! Note that you should first take out the value set (allValue) of the big dictionary, use objiectAtIndex: to get the small dictionary, then find the value set of the small dictionary, and use objiectAtIndex: to find out the specific content.

Id a = [bigDic allValues] objectAtIndex:i] allValues] objectAtIndex:j]

NSLog (@ "after traversing the dictionary (set of small dictionaries)% @", a)

}

}

2015-01-15 23 OC 12 14 36.630 Jan 15 [3573 Vol 303] {

First = {

Age = 30

Name = zhangsan

Sex = M

}

Second = {

Age = 30

Name = lisi

Sex = M

}

}

2015-01-15 23 12 OC 36.632 January 15 [3573] after traversing the dictionary (Li set small dictionary) zhangsan

2015-01-15 23 OC 12V 36.633 Jan 15 [3573 Vol 303] after traversing the dictionary (Li set small dictionary)

2015-01-15 23 1215 36.633 OC January 15 [3573 Vol 303] Ergodic Dictionary (Li set Little Dictionary)

2015-01-15 23 1215 36.634 OC January 15 [3573 Vol 303] after traversing the dictionary (Li set small dictionary) lisi

2015-01-15 23 1215 36.634 OC January 15 [3573 Vol 303] after traversing the dictionary (Li set small dictionary)

2015-01-15 23 1215 36.634 OC January 15 [3573 Vol 303] Ergodic Dictionary (Li set small Dictionary) after 30

These are all the contents of the article "what are the common functions of the API library in OC?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report