7,字符串和数字
mac从文件中读取字符串
NSString *filePathName = @"/Users/Shared/ScriptTestTempFile.js";
NSError *fileError;
NSString *textFileContents = [NSString stringWithContentsOfFile:filePathName
encoding:NSASCIIStringEncoding error:&fileError];
if(fileError.code == 0)
{
NSLog(@"textfile.txt contents: %@", textFileContents);
}
else
{
NSLog(@"error(%ld): %@", fileError.code, fileError.description);
}
iOS从文件读写字符串
首先需要将文本文件拖放到Xcode中的Supporting Files目录下。当对话框弹出时,选中要将条目复制到
目标分组目录的复选框。这么做可以确保文本文件会包含。在iOS中,需要获得指向包目录的引用。由于是动态
目录路径,可以通过[[NSBundle mainBundle] resourcePath]获得指向所有资源文件所在目录的引用。
接下来通过stringWithFormat:方法构建指向文本文件的引用.
NSString *bundlePathName = [[NSBundle mainBundle] resoucePath];
NSString *filePathName = [NSString stringWithFormat:@"%@/userFile.text", bundlePathName];
其他同mac读写
mac将字符串写到文件中:想字符串对象发送writeToFile:atomically:encoding:error:消息,文件路径名就
会存储字符串对象中的内容。
NSString *filePathName1 = @"/Users/Shared/test.txt";
NSError *fileError1;
NSString *textFileContents1 = @"Content generated from a Mac Program.Was it existed?";
[textFileContents1 writeToFile:filePathName1 atomically:YES encoding:NSASCIIStringEncoding error:&fileError1];
if(fileError1.code == 0)
{
NSLog(@"test.txt was written successfully with these contents: %@", textFileContents1);
}
else
{
NSLog(@"error(%ld): %@", fileError1.code, fileError1.description);
}
iOS将字符串写到文件中:
同样的对于每个引用来说,文档目录是动态生成的,因此可以通过以下示列来获得之喜爱那个文档目录的引用。
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePathName = [NSString stringWithFormat:@"%@/textfile.text", documentDirectory];
其余步骤与mac写文件相同
mac从文件中读取字符串
NSString *filePathName = @"/Users/Shared/ScriptTestTempFile.js";
NSError *fileError;
NSString *textFileContents = [NSString stringWithContentsOfFile:filePathName
encoding:NSASCIIStringEncoding error:&fileError];
if(fileError.code == 0)
{
NSLog(@"textfile.txt contents: %@", textFileContents);
}
else
{
NSLog(@"error(%ld): %@", fileError.code, fileError.description);
}
iOS从文件读写字符串
首先需要将文本文件拖放到Xcode中的Supporting Files目录下。当对话框弹出时,选中要将条目复制到
目标分组目录的复选框。这么做可以确保文本文件会包含。在iOS中,需要获得指向包目录的引用。由于是动态
目录路径,可以通过[[NSBundle mainBundle] resourcePath]获得指向所有资源文件所在目录的引用。
接下来通过stringWithFormat:方法构建指向文本文件的引用.
NSString *bundlePathName = [[NSBundle mainBundle] resoucePath];
NSString *filePathName = [NSString stringWithFormat:@"%@/userFile.text", bundlePathName];
其他同mac读写
mac将字符串写到文件中:想字符串对象发送writeToFile:atomically:encoding:error:消息,文件路径名就
会存储字符串对象中的内容。
NSString *filePathName1 = @"/Users/Shared/test.txt";
NSError *fileError1;
NSString *textFileContents1 = @"Content generated from a Mac Program.Was it existed?";
[textFileContents1 writeToFile:filePathName1 atomically:YES encoding:NSASCIIStringEncoding error:&fileError1];
if(fileError1.code == 0)
{
NSLog(@"test.txt was written successfully with these contents: %@", textFileContents1);
}
else
{
NSLog(@"error(%ld): %@", fileError1.code, fileError1.description);
}
iOS将字符串写到文件中:
同样的对于每个引用来说,文档目录是动态生成的,因此可以通过以下示列来获得之喜爱那个文档目录的引用。
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePathName = [NSString stringWithFormat:@"%@/textfile.text", documentDirectory];
其余步骤与mac写文件相同