开发者

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:

  1. Declare a file scope NSMutableData instance.
  2. in your connection:DidRecieveData: callback append "data" to the previously created NSMutableData instance.
  3. In connectionDidFinishLoading: callback use initWithData:encoding: of NSString and pass the NSMutableData instance and NSUTF8StringEncoding as parameters.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜