-[NSCFDictionary length]: unrecognized selector

3

我在这段代码中遇到了问题:

NSDictionary * imagen = [[NSDictionary alloc] initWithDictionary:[envio resultValue]];
NSString *imagenS = [imagen valueForKey:@"/Result"];

[Base64 initialize];
NSData * imagenDecode = [Base64 decode:imagenS];

NSLog(@"%@", [imagenS length]);

//SAVE IMAGE

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  

NSString *docDirectory = [sysPaths objectAtIndex:0]; 

NSString *filePath = [NSString stringWithFormat:@"%@david.png",docDirectory]; 

[imagenDecode writeToFile:filePath atomically:YES]; 

[envio resultValue] --> 返回一个包含一张图片以 Base 64 编码的 NSDictionary。

我想要解码并保存这张图片,但在我的控制台上显示了以下消息:

2011-08-23 20:15:36.539 WSStub[39226:a0f] -[NSCFDictionary length]: unrecognized selector sent to instance 0xd00ee0

Base 64编码是:
//
//  Base64.m
//  CryptTest
//
//  Created by Kiichi Takeuchi on 4/20/10.
//  Copyright 2010 ObjectGraph LLC. All rights reserved.
//

#import "Base64.h"


@implementation Base64

#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))

static char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char decodingTable[128];

+ (void) initialize 
{
    if (self == [Base64 class]) 
    {
        memset(decodingTable, 0, ArrayLength(decodingTable));
        for (NSInteger i = 0; i < ArrayLength(encodingTable); i++) {
            decodingTable[encodingTable[i]] = i;
        }
    }
}


+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength 
{
    if ((string == NULL) || (inputLength % 4 != 0)) 
    {
        return nil;
    }

    while (inputLength > 0 && string[inputLength - 1] == '=') 
    {
        inputLength--;
    }

    NSInteger outputLength = inputLength * 3 / 4;
    NSMutableData* data = [NSMutableData dataWithLength:outputLength];
    uint8_t* output = data.mutableBytes;

    NSInteger inputPoint = 0;
    NSInteger outputPoint = 0;
    while (inputPoint < inputLength) 
    {
        char i0 = string[inputPoint++];
        char i1 = string[inputPoint++];
        char i2 = inputPoint < inputLength ? string[inputPoint++] : 'A'; /* 'A' will decode to \0 */
        char i3 = inputPoint < inputLength ? string[inputPoint++] : 'A';

        output[outputPoint++] = (decodingTable[i0] << 2) | (decodingTable[i1] >> 4);
        if (outputPoint < outputLength)
        {
            output[outputPoint++] = ((decodingTable[i1] & 0xf) << 4) | (decodingTable[i2] >> 2);
        }
        if (outputPoint < outputLength)
        {
            output[outputPoint++] = ((decodingTable[i2] & 0x3) << 6) | decodingTable[i3];
        }
    }

    return data;
}

+ (NSData*) decode:(NSString*) string 
{
    return [self decode:[string cStringUsingEncoding:NSASCIIStringEncoding] length:[string length]];
}

@end

1
看起来 imagenS 是一个 NSDictionary 而不是 NSString。你能把打印 imagen 的结果发出来吗? - albertamg
2个回答

3

这条线

NSString *imagenS = [imagen valueForKey:@"/Result"];

返回的是一个字典,而不是字符串。你需要检查数据源来确定这是否正确。


2

你的 NSLog() 调用有误。如果要显示长度,应该是:

NSLog(@"%lu", [imagenS length]);

但这可能不是问题所在。

你似乎在一个 NSDictionary 上调用了 length。很难确定您在哪里进行了此操作,因为您没有展示发生此操作的代码片段。它可能是 imagenS 不是 NSString,而是一个 NSDictionary

尝试执行以下操作:

NSLog(@"%@", [imagenS class]);

查看显示的内容,如果可能会告诉您imagenS不是字符串,而是NSCFDictionary或类似物。


顺便说一下,NSCFDictionaryNSDictionary的子类之一,实际上实现了主类的不同版本。这被称为类簇。


这行代码引发了错误:NSLog(@“%@”,[imagenS length]); - David
你确定 [imagen valueForKey:@"/Result"] 返回的值是一个NSString类型吗? - Chaitanya Gupta
@Chaitanya:我猜他可以非常确定imagenS不是一个字符串。<g> - Rudy Velthuis
@David:如果你执行以下代码会发生什么情况呢?NSLog(@"%@", [imagenS class]); - Rudy Velthuis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接