Small grammar for iOS

Source: Internet
Author: User

Structural body

Nsrange represents a range such as: Nsrange rg={3,5};3 is the starting position, 5 is the length

Nspoint (Cgpoint) represents a point such as: Nspoint P2=cgpointmake (10, 15);

Nssize (cgsize) indicates size such as: Cgsize S2=cgsizemake (10, 15);

Nsrect represents a rectangle such as: Nsrect R=nsmakerect (10, 5, 100, 200);

These are all structural bodies, which can be accessed with specific parameters

Date

NSDate *date1=[nsdate date];//Get the current date

NSLog (@ "%@", date1); Result: 2014-07-16 07:25:28 +0000

Date formatting
NSDateFormatter *formater1=[[nsdateformatter Alloc]init];
[Email protected] "YY-MM-DD";
NSString *datestr1=[formater1 stringfromdate:date1];
NSLog (@ "%@", DATESTR1); Results: 14-07-16
string conversion to date
NSDate *date6=[formater1 datefromstring:@ "14-02-14 11:07:16"];
NSLog (@ "%@", Date6); Result: 2014-02-14 03:07:16 +0000

string

Immutable string NSString

[@ "Hello world!" uppercasestring];//converted to uppercase

[@ "Hello world!" lowercasestring];//converted to lowercase

[@ "Hello world!" capitalizedstring];//First letter uppercase

[@ "abcdef" hasprefix:@ "AB"];//start position with AB return Yes and no

[@ "abcdef" hassuffix:@ "EF"];//tail ends with EF return Yes and no

[@ "ABCdef" substringfromindex:3];//from the third index (including the character of the third index) intercept to the last

[@ "abcdef" substringtoindex:3];//is truncated from 0 to the third index (excluding the character corresponding to the third index)

[@ "ABCdef" Substringwithrange:nsmakerange (2, 3)];//starting from the third position, length 3

Nsarray *array1=[str1 componentsseparatedbystring:@ "."];/ /String Segmentation

variable character nsmutablestring

Nsmutablestring *str1= [[Nsmutablestring alloc] initwithcapacity:10];
[str1 setstring:@ "Hello"];//set string

[Str1 appendstring:@ ", world!"];/ /Append String

[Str1 appendformat:@] my age is%i. Dear,i love you. ", 18];

Replace string
Nsrange range=[str1 rangeofstring:@ "Dear"];
[str1 replacecharactersinrange:range withstring:@ "Honey"];
[str1 insertstring:@ "My name is Kenshin." Atindex:12]; //Insert String

[str1 deletecharactersinrange:[str1 rangeofstring:@ "My name is Kenshin."]];/ /delete A string of the specified range

Array

non-variable group Nsarray

NSObject *obj=[[nsobject Alloc]init];
Nsarray *array1=[nsarray arraywithobjects:@ "abc", obj,@ "CDE", @ "OPQ", @25, Nil];//nsarray is immutable, so it must be assigned at initialization and ends with nil, The number is nil, not counted.
NSLog (@ "%zi", array1.count);//array length, results: 5
NSLog (@ "%i", [array1 containsobject:@ "CDE"]);//contains an object, results: 1
NSLog (@ "%@", [Array1 lastobject]);//Last object, result: 25
NSLog (@ "%zi", [array1 indexofobject:@ "abc"]);//Object location: 0

Traversal of an array

Method 1

for (int i=0,len=array1.count;i<len;++i) {
NSLog (@ "Method1:index%i is%@", I,[array1 objectatindex:i]);
}

Method 2
for (ID obj in array) {
NSLog (@ "Method2:index%zi is%@", [array indexofobject:obj],obj);
}

Method 3, using the code block method
[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {
NSLog (@ "Method3:index%zi is%@", idx,obj);
if (idx==2) {//when idx=2 set *stop to Yes to stop traversal
*stop=yes;
}
}];

/Method 4, using iterators

Nsenumerator *enumerator=[array reverseobjectenumerator];//gets a reverse iterator

ID Obj2=nil;
while (Obj2=[enumerator Nextobject]) {
NSLog (@ "method4:%@", obj2);
}

Attention:

Nsarray can only hold objects, cannot hold basic data types, usually we can be converted by adding @ before the basic data type;

The elements in the array must be followed by nil to indicate the end of the data;

Variable group Nsmutablearray

Nsmutablearray *array1=[nsmutablearray arraywithobjects:@ "AA", @ "BB", @ "CC", nil];

[Array1 addobject:@ "VV"];//add an element

[Array1 removeobject:person3];//Delete an element

[Array1 removelastobject];//deletes the last element,

[Array1 removeallobjects];//Delete so element

Attention:

The elements in a variable group must be followed by nil to indicate the end of the data;
Adds an object to a mutable array, at which point the reference counter of the object is incremented by 1 when the object removes its reference counter from the mutable array minus 1. The Releaes method of each object is called in turn when the entire array is destroyed.

Small grammar for iOS

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.