简体   繁体   English

Parse.com-从.txt检索文本

[英]Parse.com - retrieve text from .txt

I'm trying to retrieve some text from a file that I uploaded to Parse, but anything that I try seems not to work. 我正在尝试从我上传到Parse的文件中检索一些文本,但是我尝试执行的任何操作似乎都无法正常工作。 I tried using the example that Parse gave, but I couldn't make it work. 我尝试使用Parse给出的示例,但无法使其正常工作。 Can somebody help or explain to me how I can do that? 有人可以帮我解释一下我该怎么做吗?

This is the example: 这是示例:

PFFile *applicantResume = anotherApplication[@"applicantResumeFile"]; 
NSData *resumeData = [applicantResume getData];

Thanks 谢谢

You need to convert the data back to a string, try this. 您需要将数据转换回字符串,请尝试此操作。

Save

PFObject * anotherApplication = [[PFObject alloc] initWithClassName:@"Resumes"];
NSData *data = [@"Working at Parse is great!" dataUsingEncoding:NSUTF8StringEncoding];
PFFile *file = [PFFile fileWithName:@"resume.txt" data:data];
anotherApplication[@"applicantResumeFile"] = file;
[anotherApplication save];

Open 打开

[anotherApplication fetchIfNeeded];
PFFile *applicantResume = anotherApplication[@"applicantResumeFile"];
NSData *resumeData = [applicantResume getData];
NSString* dataStr = [[NSString alloc] initWithData:resumeData encoding:NSUTF8StringEncoding];
NSLog(@"Received string: %@", dataStr);

Don't forget that in practice, it's best to use 'getDataInBackgroundWithBlock:' , 'saveInBackgroundWithBlock:' , and 'fetchIfNeededInBackgroundWithBlock:' 在实践中不要忘记,最好使用“ getDataInBackgroundWithBlock:”,“ saveInBackgroundWithBlock:”和“ fetchIfNeededInBackgroundWithBlock:”

A note on blocks: 关于块的注释:

NSLog(@"Will Run 1st: %@", reflex.description); // will be null
[applicantResume getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    NSString* dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    reflex.description = dataStr;
    NSLog(@"Will Run 3rd: %@", reflex.description); // will contain string.
}];
NSLog(@"Will Run 2nd: %@", reflex.description); // will be null
PFFile *applicantResume = object[@"applicantResumeFile"];
NSData *resumeData = [applicantResume getData];
NSString* dataStr = [[NSString alloc] initWithData:resumeData encoding:NSUTF8StringEncoding];
reflex.description = dataStr; 

It workы 它起作用

When I use getData it doesnt 当我使用getData时,它不会

PFFile *applicantResume = object[@"applicantResumeFile"];
[applicantResume getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    if (!error) {
        NSString* dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        reflex.description = dataStr;

    }
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM