i am new to xcode and objective c.
firstly i have created a file in documents directory and it is created and now what i need is how can i read the data from that file like whether the file is containing a particular string or not
How can i get that
i am new to xcode and objective c.
firstly i have created a file in documents directory and it is created and now what i need is how can i read the data from that file like whether the file is containing a particular string or not
How can i get that
您可以在这里按照我的回答,保存和检索 plist 文件
与您编写的方式相同,您可以检索数据,假设您想从文档文件夹中获取图像,然后查看此代码 -
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]];
NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
//fileName is your image path and image.jpg is your image name which is saved in documents folder...
NSData *retrieveData = [NSData dataWithContentsOfFile:fileName];
UIImage *newImage = [UIImage imageWithData: retrieveData];
在这种情况下,而不是
[data writeToFile:fileName atomically:YES];
你必须写
NSData *retrieveData = [NSData dataWithContentsOfFile:fileName];
线。你可以得到所有类型的数据。
谢谢你!!
以下方法将文件的内容获取到一个字符串。
+ (id)stringWithContentsOfFile:(NSString *)path
usedEncoding:(NSStringEncoding *)enc
error:(NSError **)error
然后,您可以通过以下方式确定返回的字符串是否包含指定的子字符串:
NSRange textRange;
textRange =[string rangeOfString:@"ohai"];
if(textRange.location != NSNotFound) {
//String contains substring "ohai"
}
希望这可以帮助。
您可以通过以下方式从文档文件夹中的文件中读取数据:
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/<pathofFile>"];
NSString *fileContent = [[NSString alloc] initWithContentOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
现在,您可以使用 NSString 方法在 fileContent 中搜索子字符串。