1. 简化class调用
原文:
Person *raymond;
raymond = [Person alloc];
raymond = [raymond init];
short cut后:
Person *raymond = [[Person alloc]init];
2. 打印:
int j = 123;
NSLog{@"%f", j};
3. loop:
int x;
for(x=1/*start condition*/; x<=10/*loop condition*/; x = x + 1;){}
4. input:
scanf("%i", &userNum);
5. 简化class(tutorior 25 http://www.youtube.com/watch?v=o4xImiOzcfM&feature=related)
1)在interface
@property int age, weight
2) 在implementation
@synthesize age, weight
3) 在main
raymond.age = 20;
raymond.weight = 65;
50.define
#define PI 3.14159
#define TWOPIE 2.0*PI
pay attension at the end of define, there isn't ';' symble.
51.
#define EVEN(x) x%2
in main
if(EVEN(15))
http://www.blogger.com/post-edit.g?blogID=3925725250678749477&postID=3288221837432179234#
53.
framework string
NSString *s = @"this is object";
NSLog(@"%@",s);
the "%@" 是代表所有的object(实例)。
%i 是int
%f 是float
54. subStrings
NSString *s = @"Cat dont like eat bananas";
NSSring *n;
n = [s substringToIndex:10];
取得第十个字符;
n = [s substringFromIndex:5];
从第五个开始取得字符直到结尾;
n = [s substringWithRange:NSMakeRange(5, 11)];
从第五个开始,一共取得11个字符;
NSRange range = [s rangeOfString:@"eat"];
NSLog(@"Location is %i and length is %i", range.location, range.length);
这里的location是在什么地方开始的,length是eat这个字符的长度;
55. Mutable string
NSString *dog = @"Hotdog? I thought you said pumpking";
NSMutableString *mute;
mute = [NSMutableString stringWithString: dog];
[mute insertString: @" sauce" atIndex: 6];
第一个参数insertString是想插入什么,第二个是在什么地方插入。
[mute appendString: @" My bad!"];
appendString 插入到最后。
[mute deleteCharactersInRange:NSMakeRange(12, 20)];
deletCharactersInRange 第12个字符开始,20个字符;
[mute setString: @"I am a new string mofo!"];
set mute 一个新的string I am a new string mofo!
[mute replaceCharactersInRange:NSMakeRange(11, 12) withString:@"mother!"];
用mother这个string替换从11个字符开始的12个字符;
NSString *old = @"mother";
NSString *new = @"baby seal";
NSRange therange = [mute rangeOfString:old];
[mute replaceCharatersInRange:therange withString:new];
57. Array inMutable array(固定的数组)
NSArray *food = [NSArray arrayWithObjects:@"apples", @"bacon", @"corn", @"elfs", @"fidge", nil];
NSLog(@"%@", [food objectAtIndex:2]);
结果是bacon
for(int i=0; i<5; i++){
NSLog(@"items at index %i is %@", i, [food objectAtIndex:i] );
}
58. Mutable array()
NSMutableArray *changeme = [NSMutableArray arrayWithCapacity:25];
for(int i=6; i<=100; i+=2){
[changeme addObject:[NSNumber numberWithInteger:i]];
}
输入数组
arrayWithCapacity:25 这个是如果数组大于25,就自动增加.
for(int x=0; x<[changeme count]; x++){
NSLog(@"item here is : %i", [[changeme objectAtIndex:x]integerValue ]);
}
59. Dictionary Objects
NSMutableDictionary *mydic = [NSMutableDictionary dictionary]; //give a empty dictionary
[mydic setObject:@"when you spray yourself with frebreeze" forKey :@"febreeze shower"]; //add key
[mydic setObject:@"gamer who lacks experience" forKey :@"n00b"];
[mydic setObject:@"worst food ont he pleanter" forKey :@"sushi"];
NSLog(@"%@", [mydic objectForKey:@"sushi"]);
60. file
NSString *testfile = @"testerfile";
NSFileManager *manager;
manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:testfile]==NO){
NSLog();
return 1;
}
61.Copy and Rename files
if([manager copyItemAtPath: testfile toPath: @"newfile" error:NULL]==NO){
NSLog(@"can not COPY the file");
return 2;
}
//toPath: 新文件的文件名是路径,可以是@"folder/newfile"
//error:NULL 如果出现错误,不做任何事;
if([manager moveItemAtPath: @"newfile" toPath: @"newfile2" error:NULL]==NO){
NSLog(@"Cannot rename the file");
return 3;
}
62 file attributes
//get size of the file
NSDictionary *mydic;
if((mydic=[manager attributesOfItemAtPath:@"newfile2" error:NULL])==nil){
NSLog(@"Could not get file attributes");
return 4;
}else{
NSLog(@"File is %i bytes", [[mydic objectForKey:NSFileSize]intValue]);
}
63. Delete and Print Files
//delete a file
[manager removeItemAtPath: @"baby" error:NULL];
//print a file
NSLog(@"%@", [NSString stringWithContentsOfFile: testfile encoding: NSUTFBStringEncoding errorNULL]);
//stringWithContentsOfFile 想要显示什么文件
//encoding 想怎么显示
64. Directory(Folder)
NSFileManager *manager = [NSFileManager defaultManager];
NSString *path;
//get current directory
path = [manager currentDirectoryPath];
NSLog(@"%@", path);
//create a new directory
if([manager createDirectoryAtPath: @"newdir" withIntermediateDirectories: NO attributes:nill error:NULL]==NO){
NSLog(@"could not create a new directory);
return 1;
}
//rename directory
[manager moveItemAtPath: @"newdir" toPath: @"newdir2" error:NULL];
65. Read and Write File
NSFileHandle *fin, *fout;
//fin: file input; fout: file output;
NSData *buffer;
//open testfile for reading
fin = [NSFileHandle fileHandleForReadingAtPath:@"testfile"];
[[NSFileManager defaultManager]createDirectoryAtPath:@"newfile" contents:nil attibutes:nil];
fout = [NSFileHandle fileHandleForWritingAtPath:@"newfile"];
//truncate file
[fout truncateFileAtOffset:0];
buffer=[fin readDataToEndOfFile];
[fout writeData: buffer];
[fin closeFile];
[fout closeFile];
No comments:
Post a Comment