iPhone, NSURLConnection > didReceiveData:(NSData *)data, How to check result data
I use url connection (http).
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *filePath; /* .../link.plist */
[data writeToFile:filePath atomically:YES];
}
After connection, I check result file (link.plist)
- (void)checkLinkResult {
NSString *filePath; //link.plist
NSString *result = [[NSString alloc] initWithContentsOfFile:filePath];
}
It works fine.
But I want to check Result String, directly, without making file.
"NSData -> file -> NSString" (now) ====> "N开发者_高级运维SData -> NSString" (i want)
Help me plz.
It's dependent your data.
If your data is Image
UIIMage * image = [[UIImage alloc] initWithData:Receivedata
If data is string
NSMutableString *string = [[NSMutableString alloc] initwithData:Receivedata encoding:nil]
In didReceiveData write to an NSMutableData object, then in checkLinkResult create a string from the NSMUtableData using the appropriate encoding.
It's actually how it's done in the apple NSConnection tutorial.
Follow the following steps:
- Declare a file scope NSMutableData instance.
- in your connection:DidRecieveData: callback append "data" to the previously created NSMutableData instance.
- In connectionDidFinishLoading: callback use initWithData:encoding: of NSString and pass the NSMutableData instance and NSUTF8StringEncoding as parameters.
精彩评论