NSRange、NSPoint、NSSize、NSString、NSMutableString、NSArray
一、NSRange 创建范围结构体。
方法:NSMakeRange(参1,参2)。location和length。
二、集合数据类型。
1,NSPoint 创建一个点。
方法:NSMakePoint(参1,参2)。x和y。
2,NSSize 创建长和宽。
方法:NSMakeSize(参1,参2)。width和height。
三、NSString 字符串。
1,创建。
1)类方法:+(id)stringWithString:字符串。
如:[NSString stringWithString:@"beijing"];
2)类方法:+(id)stringWithFormat:字符串、格式控制符。
如:[NSString stringWithFormat:@"wang%i",3];
3)实例方法:-(id)intWithString:字符串。
如:[[NSString alloc] initWithString:@"hello"];
4)实例方法:-(id)initWithFormat:字符串、格式控制符。
如:[NSString alloc] initWithFormat:@"I hava %i sons",3];
5)最直接的方法:NSString *str = @"i am a getal man";
2、求字符串长度:length方法。
如:[字符串对象 length];
3、字符串比较方法。
1)全方位比较、比较字符串是否相同。
-(BOOL) isEqualToString:
如:[字符串对象1 isEqualToString:字符串对象2];
2)-(枚举类型的值)compare:字符串。
注意:此方法比较的是:字符串中每个字母的ASCII码值。
该方法的返回值类型是:三个枚举类型的值,切记:不事BOOL值。
返回的三个枚举值为:
NSOrderedAscending:主调小于被调。
NSOrderedSame:相同。
NSOrderedDescending:主调大于被调。
如:[字符串1 compare:字符串2];注意:不能直接用if判断哈。
得这样:if ([字符串1 compare:字符串2] == NSOrderedSame)
3)带多个参数的compare方法。
参数为一下三种情况:
1)NSCaseInsensitiveSerch:不区分大小写(常用于不区分大小写时)。
2)NSNumberSerch:按字符串长度比较(常用于数字字符串之间的比较)。
3)NSLiteralSerch:区分大小写。
4,判断字符串的开头结尾。
-(BOOL)hasPrefix:字符串1。是否以字符串1开头?
-(BOOL)hasSuffix:字符串2。是否以字符串2结尾?
5、字符串查找。
-(NSRange)rangeOfString:字符串3。
注意;返回的是范围结构体。
6、大小写转换。
-(NSString*)uppercaseString。转换成大写。
-(NSString*)lowercaseString。转换成小写。
注意:转换后本来的字符串不会变化,它是通过返回值给出的结果。
四、NSMutableString 可变字符串。
1、创建。
方法:+(id)stringWithCapacity:大小。
注意:此时的大小只是个最优值。可以为该字符串分配如此“大小”的内存空间。这样的话,后续操作会很快。
记得:也可以用alloc、init创建并初始化字符串的。
2、添加字符串。
appendString:字符串7。表示:在调用者尾部添加字符串7.
appendFormat:字符串8、格式控制符。在尾部添加字符串8.
3、插入字符串。
-(void)insertString:字符串3 atIndex:表示:在某位置插入某字符串。
4、设置字符串。
-(void)setString:字符串2。将调用者设置成字符串2。
5、删除。
-(void)deleteCharactorsInRange:NSRange(参1,参2)。表示将参1和参2范围内的字母删除。
五、集合。
一、NSArray 不可变数组(不能添加、删除元素),存储对象的有序数列,存放oc对象,不能存储nil。
1,创建。
arrayWithObjects:字符串1,字符串2,···,nil。
如:[NSArray arrayWithObjects: @"one", @"two", nil];
2,求元素个数。
count方法。
如:[对象 count];
3,查找。
objectAtIndex方法。
如:[对象 objectAtIndex:3];
二、NSMutableArray 可变数组。(可变哦、亲。可以添加、删除元素的)
1,创建。
arrayWithCapacity:大小。
2,添加。
addObject方法。(在数组尾部添加的哦、亲)
3,删除。
removeObjectAtIndex:位置。表示:删除某具体位置的元素。
4,插入。
insertObject:anIndex:方法。表示:在某个具体位置添加某个具体元素。
5,替换。
replaceObjectAtIndex:withObject:方法。表示:删除某位置上的元素。
*/
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool {
//创建范围结构体。
NSRange range1;
//方式一:为结构体重的变量复制。注意:只有location和length。
range1.location = 17;
range1.length = 4;
NSLog(@"range1.location=%i,range1.length=%i", (int)range1.location, (int)range1.length);
//方式二:整体赋值。
NSRange range2 = {12, 3};
NSLog(@"range2.location=%i,range2.length=%i", (int)range2.location, (int)range2.length);
//方式三:用NSMakeRange()函数赋值。最常用的方式。
NSRange range3 = NSMakeRange(14, 4);
NSLog(@"range3.location=%i,range3.length=%i", (int)range3.location, (int)range3.length);
//创建集合数据结构体NSPoint。
NSPoint p1;
//为结构体的变量赋值。注意:只有X和Y。
p1.x = 8;
p1.y = 8;
NSLog(@"p1.x = %f;p1.y = %f",p1.x,p1.y);
//整体赋值。
NSPoint p2 = {9,9};
NSLog(@"p2.x = %f;p2.y = %f",p2.x,p2.y);
//用NSMakePoint()方法赋值。最常用的方式。
NSPoint p3 = NSMakePoint(10, 10);
NSLog(@"p3.x = %f;p3.y = %f",p3.x,p3.y);
////创建集合数据结构体NSSize。
NSSize s1;
//为结构体的变量赋值。注意:只有width和height。
s1.width = 11;
s1.height = 11;
NSLog(@"s1.x = %f;s1.y = %f",s1.width, s1.height);
//整体赋值。
NSSize s2 = {12, 12};
NSLog(@"s2.x = %f;s2.y = %f",s2.width, s2.height);
//用NSMakeSize()方法赋值。最常用的方式。
NSSize s3 = NSMakeSize(13, 13);
NSLog(@"s3.x = %f;s3.y = %f",s3.width, s3.height);
//创建集合图形:矩形NSRect。并用NSMakeRect()为其赋值。
NSRect rect = NSMakeRect(p1.x, p1.y, s1.width, s1.height);
//打印图形。注意:origin和size是该图形的两个变量。
NSLog(@"该矩形是:%f,%f,%f,%f。",
rect.origin.x,rect.origin.y, rect.size.width,rect.size.height);
//用5中方法创建字符串。
NSString *str1 = [NSString stringWithString:@"Wang Jian Yu"];
NSString *str2 = [NSString stringWithFormat:@"wang%i", 3];
NSString *str3 = [[NSString alloc] initWithFormat:@"wang%i", 4];
NSString *str4 = [[NSString alloc] initWithString:@"zai beijing"];
NSString *str5 = @"wang jian yu";
NSLog(@"str1的长度为:%i", (int)[str1 length]);//打印数组的长度length。
if (![str1 isEqualToString:str5]) {//比较字符串是否完全相同。
NSLog(@"e:::str1 he str5 xiangdeng.");
}
//比较字符串中所有字母的ASCII码值是否相同。此时和isEqualToString作用一样。
if ([str1 compare:str5] != NSOrderedSame) {
NSLog(@"c::::str1 he str5 xiangdeng.");
}
//带参数的compare方法。注意该方法返回的是枚举值、不是BOOL值。相当注意。
if ([str1 compare:str5 options:NSCaseInsensitiveSearch] == NSOrderedSame) {
NSLog(@"c::忽略大小写::str1和 str5 相等.");
}
//判断字符串开头。
if (![str2 hasPrefix:str3]) {
NSLog(@"没有前缀。");
}
//判断字符串结尾。
if (![str3 hasSuffix:str4]) {
NSLog(@"没有后缀。");
}
//查找字符串,返回值是NSRange。
NSRange r1 = [str2 rangeOfString:@"wang"];
NSLog(@"从哪个位置开始:%i,长度是:%i。", (int)r1.location,(int) r1.length);
//将字符串中的字母转换成大写。
NSLog(@"%@", [str4 uppercaseString]);
//将字符串中的字母转换成小写。
NSLog(@"%@", [str1 lowercaseString]);
NSLog(@"----------------------------------------------------------");
//创建可变化的字符串。
NSMutableString *ms = [NSMutableString stringWithCapacity:30];
//向字符串的尾部添加字符串。
[ms appendString:@"you have "];
//向字符串的尾部添加字符串。
[ms appendFormat:@"%i pigs", 3];
NSLog(@"ms = %@",ms);
//向字符串中插入字符串。
[ms insertString:@"more than" atIndex:9];
NSLog(@"ms = %@",ms);
//删除指定范围内的字符串。
[ms deleteCharactersInRange:NSMakeRange(9, 10)];
NSLog(@"ms = %@",ms);
//将对象设置成新的字符串。
[ms setString:@"A new string"];
NSLog(@"%@", ms);
NSLog(@"----------------------------------------------------------");
//创建数组。
NSArray *ar = [NSArray arrayWithObjects:@"hello",@"world",@"hello",@"Objective-C", nil];
NSLog(@"count=%i", (int)[ar count]);//打印数组中元素的个数。
//遍历数组。
for (int i = 0; i < [ar count]; i++) {
NSLog(@"%@", [ar objectAtIndex:i]);
}
//创建可变数组。
NSMutableArray *ma = [NSMutableArray arrayWithCapacity:5];
//想数组中添加元素。
[ma addObject:@"string1"];
[ma addObject:@"string2"];
[ma addObject:@"string3"];
//快速枚举法遍历数组。
for (NSArray *arr in ma) {
NSLog(@"%@", ma);
}
//查找数组中0位置的元素。
NSLog(@"%@",[ma objectAtIndex:0]);
//在数组中的交表为2 的位置上插入字符串。
[ma insertObject:@"string4" atIndex:2];
NSLog(@"%@",[ma objectAtIndex:2]);
NSLog(@"%i",(int) [ma count]);
//将数组中角标为1 的元素替换为:string5
[ma replaceObjectAtIndex:1 withObject:@"string5"];
//查找数组中角标为1的元素,并打印。
NSLog(@"%@",[ma objectAtIndex:1]);
//删除数组中角标为0 的元素。
[ma removeObjectAtIndex:0];
for (NSArray *arr in ma) {
NSLog(@"%@", ma);
}
//删除数组中的所有元素。(将数组清空)
[ma removeAllObjects];
for (NSArray *arr in ma) {
NSLog(@"%@", ma);
}
NSLog(@"没哟元素了.");
}
return 0;
}