首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutabledata字节容量

NSMutabledata字节容量
EN

Stack Overflow用户
提问于 2012-12-08 02:00:10
回答 1查看 367关注 0票数 0

可能重复: NSMutableData dissapearing

当我有一个NSMutableData变量时,在我的程序中,当它有90810字节时,它总是转储它的内容。数据中的字节数是否会导致数据丢失?这是密码。

代码语言:javascript
复制
- (void)fetchEntries
{
        // Construct a URL that will ask the service for what you want 
    NSURL *url = [NSURL URLWithString: @"http://www.nhara.org/scored_races-2013.htm"];//

    // Put that URL into an NSURLRequest
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Create a connection that will exchange this request for data from the URL 
    connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data 
{
    // Add the incoming chunk of data to the container we are keeping 
    // The data always comes in the correct order 
     [xmlData appendData:data];


    NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]autorelease];
    NSLog(@"xmlCheck = %@", xmlCheck);

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error= %@",error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn {

    // We are just checking to make sure we are getting the XML 
    NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"xmlCheck2 = %@", xmlCheck);
}

这是它被切断的地方,但这改变了

代码语言:javascript
复制
<td style="font-size: 12.0pt; color: black; font-weight: 400; text-decoration: none; text-underline-style: none; font-family: Arial Narrow; font-style: normal; text-align: general; vertical-align: bottom; white-space: nowrap; border-left: medium none; border-right: 1.0pt solid windowtext; border-top: medium none; border-bottom: 1.0pt solid windowtext; padding-left: 1px; padding-right: 1px; padding-top: 1px; background: #EAF1DD">
    King Pine</td>
    <td style="font-size: 12.0pt; color: black; font-weight: 400; text-decoration: none; text-underline-style: none; font-family: Arial Narrow; text-align: right; font-style: normal; vertical-align: bottom; white-space: nowrap; border-left: medium none; border-right: 1.0pt solid windowtext; border-top: medium none; bor
2012-12-07 19:35:48.652 race tracker[2357:c07] xmlCheck = (null)
2012-12-07 19:43:28.914 race tracker[2357:c07] xmlCheck2 = (null)
2012-12-07 19:43:28.921 race tracker[2357:c07] (
)

我试图理解为什么我的NSMutableData不能工作,所以对NSMutableData的解释会有所帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-08 02:23:22

NSMutableData将增长到任意大小。

运行curl http://www.nhara.org/scored_races-2013.htm > file.txt后,我可以告诉您,服务器返回的文件正好有90810字节长。这就是为什么你要拿回那么多的字节。填充的不是可变的数据对象,而是90810正好是正确的长度。

你的问题是:

代码语言:javascript
复制
[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]

您的服务器返回的字符串无效UTF-8。要验证这一点,请尝试NSASCIIStringEncoding (它是一个所有字节都是有效的编码)。您将看到您正确地积累了一个字符串。

搜索返回的文件后,具体问题是位置74,963的字节--用于打印单引号的0x92字节。看起来你想要NSWindowsCP1252StringEncoding。上传HTML的人似乎使用了微软的代码页1252。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13773693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档