-[NSCFDictionary length]:无法识别的选择器

发布于 2024-11-30 22:37:19 字数 2963 浏览 1 评论 0原文

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

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] --> 返回一个 NSDictionary,其中包含 Base 64 编码中的一张图像。

我想要解码器并保存此图像,但在我的控制台中我显示了此消息:

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

I have the next problem with this code:

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] --> return a NSDictionary with one image in Base 64 codification.

I want decoder and save this image but in my console I have showed this message:

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

The Base 64 code is:

//
//  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆夏时光 2024-12-07 22:37:19

该行

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

返回字典,而不是字符串。您必须检查您的数据源以确定这是否正确。

The line

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

is returning a dictionary, not a string. You'll have to inspect your data source to determine whether this is correct or not.

梦幻的心爱 2024-12-07 22:37:19

您的 NSLog() 调用是错误的。要显示长度,应该是:

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

但这可能不是问题所在。

您似乎在 NSDictionary 上调用 length 。很难告诉你在哪里这样做,因为你没有显示发生这种情况的代码片段。可能 imagenS 不是一个 NSString,而是一个 NSDictionary

尝试执行:

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

并查看显示的内容。如果可能告诉您imagenS不是一个字符串,而是一个NSCFDictionary或类似的东西。


FWIW,NSCFDictionaryNSDictionary 的子类之一,它实际上实现了主类的不同版本。这称为类簇。

Your NSLog() call is wrong. To show a length, it should be:

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

But that is probably not the problem.

You seem to be invoking length on an NSDictionary. Hard to tell where you do that, since you don't show the piece of code where that happens. It could be that imagenS is not an NSString, but an NSDictionary instead.

Try to do:

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

and see what is displayed. If probably tells you that imagenS is not a string, but an NSCFDictionary or similar instead.


FWIW, NSCFDictionary is one of the subclasses of NSDictionary that actually implement the different versions of the main class. This is called a class cluster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文