Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将NSMutableArray转换为字符串并返回_Iphone_Nsmutablearray_Nsarray - Fatal编程技术网

Iphone 将NSMutableArray转换为字符串并返回

Iphone 将NSMutableArray转换为字符串并返回,iphone,nsmutablearray,nsarray,Iphone,Nsmutablearray,Nsarray,在我当前的项目中,我面临以下问题: 该应用程序需要与我的服务器交换数据,这些数据存储在iPhone的NSMutableArray中。该数组包含NSString、NSData和CGPoint值。 现在,我认为实现这一点最简单的方法是将数组转换为正确格式的字符串,将其发送到我的服务器并存储在某个mySQL数据库中。此时,我想从服务器请求表示数组内容的字符串,然后实际将其转换回NSMutablArray 到目前为止,我试过这样的方法: NSString *myArrayString = [myArra

在我当前的项目中,我面临以下问题:

该应用程序需要与我的服务器交换数据,这些数据存储在iPhone的NSMutableArray中。该数组包含NSString、NSData和CGPoint值。 现在,我认为实现这一点最简单的方法是将数组转换为正确格式的字符串,将其发送到我的服务器并存储在某个mySQL数据库中。此时,我想从服务器请求表示数组内容的字符串,然后实际将其转换回NSMutablArray

到目前为止,我试过这样的方法:

NSString *myArrayString = [myArray description];
现在我将这个字符串发送到我的服务器,并将其存储在mySQL数据库中。那部分很好用

但是,当我从服务器收到字符串时,我无法将其转换回NSMutableArray

是否有一种方法可以轻松地将数组描述转换回数组?不幸的是,到目前为止我还没有找到任何关于这方面的资料。 也许我“序列化”数组的方法从一开始就是错误的,有一种更聪明的方法可以做到这一点


谢谢你的帮助。提前感谢。

不要为此使用
说明
,因为这是为了将其转换为人类可读的“漂亮”格式。您需要的是对象的数据转储

您可能想做的是利用
NSArray
进行
NSCoding
的事实,从中获取原始字节数组--
NSData
。(这是您提到的序列化。)然后,您可以将原始字节传输到服务器,使用zip压缩,或者对base-64中的字节进行编码,以便通过HTTP发送查询字符串。要恢复阵列,您只需反转该过程

关于堆栈溢出,有很多现有的问题和答案可以帮助您解决这个问题。以下是几点:


不要使用
描述
,因为这是为了将其转换为人类可读的“漂亮”格式。您需要的是对象的数据转储

您可能想做的是利用
NSArray
进行
NSCoding
的事实,从中获取原始字节数组--
NSData
。(这是您提到的序列化。)然后,您可以将原始字节传输到服务器,使用zip压缩,或者对base-64中的字节进行编码,以便通过HTTP发送查询字符串。要恢复阵列,您只需反转该过程

关于堆栈溢出,有很多现有的问题和答案可以帮助您解决这个问题。以下是几点:


好吧,我想我有点接近了。然而,我还没有完全做到这一点,因为我在将数据转换回新数组时遇到了一些“EXC_BAD_ACCESS”

让我和你分享一些代码。下面将负责将阵列转换为NSData,将NSData转换为base64,反之亦然

@interface NSArray (dataConversion)
    + (NSArray*) arrayWithData:(NSData*) data;
    - (NSData*) convertToData;
@end

@implementation NSArray (dataConversion)


- (NSData*) convertToData {
    unsigned n= [self count]; 
NSMutableData* data = [NSMutableData dataWithLength: sizeof(unsigned)+
                       sizeof(id) *n];
unsigned* p = [data mutableBytes];
*p++= n;
[self getObjects:(void*)p];
return data;
}

+ (NSArray*) arrayWithData:(NSData*) data {
unsigned* p = (unsigned*)[data bytes];
unsigned n = *p++;
return [NSArray arrayWithObjects:(id*)p count:n];
}

@end

@interface NSData (MBBase64)

+ (id)dataWithBase64EncodedString:(NSString *)string;     //  Padding '=' characters are     optional. Whitespace is ignored.
- (NSString *)base64Encoding;
@end


static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


@implementation NSData (MBBase64)

+ (id)dataWithBase64EncodedString:(NSString *)string;
{
if (string == nil)
    [NSException raise:NSInvalidArgumentException format:nil];
if ([string length] == 0)
    return [NSData data];

static char *decodingTable = NULL;
if (decodingTable == NULL)
{
    decodingTable = malloc(256);
    if (decodingTable == NULL)
        return nil;
    memset(decodingTable, CHAR_MAX, 256);
    NSUInteger i;
    for (i = 0; i < 64; i++)
        decodingTable[(short)encodingTable[i]] = i;
}

const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
if (characters == NULL)     //  Not an ASCII string!
    return nil;
char *bytes = malloc((([string length] + 3) / 4) * 3);
if (bytes == NULL)
    return nil;
NSUInteger length = 0;

NSUInteger i = 0;
while (YES)
{
    char buffer[4];
    short bufferLength;
    for (bufferLength = 0; bufferLength < 4; i++)
    {
        if (characters[i] == '\0')
            break;
        if (isspace(characters[i]) || characters[i] == '=')
            continue;
        buffer[bufferLength] = decodingTable[(short)characters[i]];
        if (buffer[bufferLength++] == CHAR_MAX)      //  Illegal character!
        {
            free(bytes);
            return nil;
        }
    }

    if (bufferLength == 0)
        break;
    if (bufferLength == 1)      //  At least two characters are needed to produce one byte!
    {
        free(bytes);
        return nil;
    }

    //  Decode the characters in the buffer to bytes.
    bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
    if (bufferLength > 2)
        bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
    if (bufferLength > 3)
        bytes[length++] = (buffer[2] << 6) | buffer[3];
}

realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}

- (NSString *)base64Encoding;
{
if ([self length] == 0)
    return @"";

char *characters = malloc((([self length] + 2) / 3) * 4);
if (characters == NULL)
    return nil;
NSUInteger length = 0;

NSUInteger i = 0;
while (i < [self length])
{
    char buffer[3] = {0,0,0};
    short bufferLength = 0;
    while (bufferLength < 3 && i < [self length])
        buffer[bufferLength++] = ((char *)[self bytes])[i++];

    //  Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
    characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
    characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
    if (bufferLength > 1)
        characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
    else characters[length++] = '=';
    if (bufferLength > 2)
        characters[length++] = encodingTable[buffer[2] & 0x3F];
    else characters[length++] = '=';    
}

return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}

@end
messageData是我现在发送到服务器的字符串。这个很好用

现在反过来说:

NSData *arrayData = [NSData dataWithBase64EncodedString:serverResponseString]; //serverResponseString is the string returned from my server upon request

NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithData:arrayData]]; //here it crashes and points to: "return [NSArray arrayWithObjects:(id*)p count:n];"

我不是错过了什么,就是完全偏离了轨道。谢谢你的帮助。谢谢。

好吧,我想我离这里有点近了。然而,我还没有完全做到这一点,因为我在将数据转换回新数组时遇到了一些“EXC_BAD_ACCESS”

让我和你分享一些代码。下面将负责将阵列转换为NSData,将NSData转换为base64,反之亦然

@interface NSArray (dataConversion)
    + (NSArray*) arrayWithData:(NSData*) data;
    - (NSData*) convertToData;
@end

@implementation NSArray (dataConversion)


- (NSData*) convertToData {
    unsigned n= [self count]; 
NSMutableData* data = [NSMutableData dataWithLength: sizeof(unsigned)+
                       sizeof(id) *n];
unsigned* p = [data mutableBytes];
*p++= n;
[self getObjects:(void*)p];
return data;
}

+ (NSArray*) arrayWithData:(NSData*) data {
unsigned* p = (unsigned*)[data bytes];
unsigned n = *p++;
return [NSArray arrayWithObjects:(id*)p count:n];
}

@end

@interface NSData (MBBase64)

+ (id)dataWithBase64EncodedString:(NSString *)string;     //  Padding '=' characters are     optional. Whitespace is ignored.
- (NSString *)base64Encoding;
@end


static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


@implementation NSData (MBBase64)

+ (id)dataWithBase64EncodedString:(NSString *)string;
{
if (string == nil)
    [NSException raise:NSInvalidArgumentException format:nil];
if ([string length] == 0)
    return [NSData data];

static char *decodingTable = NULL;
if (decodingTable == NULL)
{
    decodingTable = malloc(256);
    if (decodingTable == NULL)
        return nil;
    memset(decodingTable, CHAR_MAX, 256);
    NSUInteger i;
    for (i = 0; i < 64; i++)
        decodingTable[(short)encodingTable[i]] = i;
}

const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
if (characters == NULL)     //  Not an ASCII string!
    return nil;
char *bytes = malloc((([string length] + 3) / 4) * 3);
if (bytes == NULL)
    return nil;
NSUInteger length = 0;

NSUInteger i = 0;
while (YES)
{
    char buffer[4];
    short bufferLength;
    for (bufferLength = 0; bufferLength < 4; i++)
    {
        if (characters[i] == '\0')
            break;
        if (isspace(characters[i]) || characters[i] == '=')
            continue;
        buffer[bufferLength] = decodingTable[(short)characters[i]];
        if (buffer[bufferLength++] == CHAR_MAX)      //  Illegal character!
        {
            free(bytes);
            return nil;
        }
    }

    if (bufferLength == 0)
        break;
    if (bufferLength == 1)      //  At least two characters are needed to produce one byte!
    {
        free(bytes);
        return nil;
    }

    //  Decode the characters in the buffer to bytes.
    bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
    if (bufferLength > 2)
        bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
    if (bufferLength > 3)
        bytes[length++] = (buffer[2] << 6) | buffer[3];
}

realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}

- (NSString *)base64Encoding;
{
if ([self length] == 0)
    return @"";

char *characters = malloc((([self length] + 2) / 3) * 4);
if (characters == NULL)
    return nil;
NSUInteger length = 0;

NSUInteger i = 0;
while (i < [self length])
{
    char buffer[3] = {0,0,0};
    short bufferLength = 0;
    while (bufferLength < 3 && i < [self length])
        buffer[bufferLength++] = ((char *)[self bytes])[i++];

    //  Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
    characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
    characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
    if (bufferLength > 1)
        characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
    else characters[length++] = '=';
    if (bufferLength > 2)
        characters[length++] = encodingTable[buffer[2] & 0x3F];
    else characters[length++] = '=';    
}

return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}

@end
messageData是我现在发送到服务器的字符串。这个很好用

现在反过来说:

NSData *arrayData = [NSData dataWithBase64EncodedString:serverResponseString]; //serverResponseString is the string returned from my server upon request

NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithData:arrayData]]; //here it crashes and points to: "return [NSArray arrayWithObjects:(id*)p count:n];"

我不是错过了什么,就是完全偏离了轨道。谢谢你的帮助。谢谢。

谢谢。因此,如果我理解正确,我应该1)将数组转换为原始字节=NSData对象?2) base64编码(1)并将其作为字符串发送到我的服务器3)将base64字符串从我的服务器转换回原始字节4)将(3)转换成一个新数组,对吗?是的,你已经得到了。但正如我所说,根据底层传输,您可以跳过base-64编码步骤。但我猜你是通过HTTP与MySQL服务器通信的,对吗?是的,实际上使用ASIHTTPRequest并通过POST提交。那么,这可能就是方法。根据base-64编码字符串的长度。。。您可能无法发送几百万个编码字符或其他任何内容:)不,离这还差得远:)谢谢。因此,如果我理解正确,我应该1)将数组转换为原始字节=NSData对象?2) base64编码(1)并将其作为字符串发送到我的服务器3)将base64字符串从我的服务器转换回原始字节4)将(3)转换成一个新数组,对吗?是的,你已经得到了。但正如我所说,根据底层传输,您可以跳过base-64编码步骤。但我猜你是通过HTTP与MySQL服务器通信的,对吗?是的,实际上使用ASIHTTPRequest并通过POST提交。那么,这可能就是方法。根据base-64编码字符串的长度。。。你可能无法发送几百万个编码字符或其他任何东西:)不,离这还差得远:)好吧,我深入研究了一下,创建了一个新项目,只是为了测试序列化和反序列化我的数组。我在数组中填充了类似于我原来项目中的对象。你猜怎么着?成功了。那么这告诉我什么呢?从我的服务器返回的字符串可能有问题?还不确定。我会调查并报告的。好吧,那很快。我想我发现了问题所在。让我解释一下:当我序列化我的数组时,我得到了一个类似于此的字符串(不带引号):“gwaawawegchwabacanaphadwhiaafczabcgqcguxsaqbacalahadanrwa4cscaddceawerwaydkcapauegaqmbiaqbobagwgwagwagwagwagwawbacakbubapbrgwdw/xsAMFkcAA==”现在,如果我告诉我的应用程序在序列化这个字符串后立即反序列化,它就可以工作了。但是,当我想反序列化与NSString*str=@“StringFromOverhere”一样的字符串时;信息技术