Encoding issue: Cocoa Error 261?

后端 未结 5 691
耶瑟儿~
耶瑟儿~ 2021-02-12 14:01

So I\'m fetching a JSON string from a php script in my iPhone app using:

NSURL *baseURL = [NSURL URLWithString:@\"test.php\"];
NSError *encodeError = [[NSError a         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-02-12 14:45

    For future reference, if you need to override the encoding, and you're working with streams without embedded NULs, something like this might be good (I've just written a rough sketch outline here, check this code is and does want you want before using it):

    NSHTTPURLResponse* resp=nil;
    NSData* jsonAsImmutableData = [NSURLConnection sendSynchronousRequest:
      [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://"]]
      returningResponse:&resp error:NULL];
    
    NSMutableData*modifiedData = [NSMutableData dataWithData:jsonAsImmutableData];
    
    char extraNulls[7] =
      {0,0,0,0,0,0,0}; // is this defensive enough for your encoding?
    [modifiedData appendBytes:extraNulls length:7];
    
    NSString* jsonAsString = [NSString stringWithCString:[modifiedData bytes]
      encoding:];
    

    But I expect your best course of action is to check that your server is both using and claiming to use UTF-8 encoding or some other Apple iPhone supported encoding.

    EDIT

    altered code comment.

提交回复
热议问题