2

我正在尝试这样做:

self.somestring = [@"hardcodedstring" stringByAppendingString:someotherstring];

但我不断得到:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteMutableData fastestEncoding]: unrecognized selector sent to instance 0x1fb930'

我没有在任何地方调用此方法,但我看到 stringByAppendingString: 在我的堆栈中调用它:

Stack: (
808221155,
806100816,
808224837,
807957033,
807851552,
812064725,
812064413,
18085, <== -[NSString stringByAppendingString:] + 109 in section LC_SEGMENT.__TEXT.__text of /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Foundation
817945012,
821160740,
821157652,
821149552,
807852041,
812070519,
807836299,
807834407,
827752032,
816118388,
816157144,
8381,
8244

) 我将如何解决这个问题,以便它附加字符串,就像它应该做的那样。谢谢,
艾萨克
编辑:要访问一些字符串,我这样做:

@property (nonatomic,retain) NSString *somestring;

在我的 ViewController.h 和:

@synthesize somestring;

在我的 ViewController.m 中。
编辑: someotherstring 来自这里:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    self.somestring = [@"hardcodedstring" stringByAppendingString:[UIImagePNGRepresentation(img) encodeBase64]]; //encodeBase64 encodes it to base64
}

更多编辑:我分手了:

2009-03-20 15:14:21.389 myproject[3467:20b] *** -[NSConcreteMutableData getCharacters:range:]: unrecognized selector sent to instance 0x1fa630
2009-03-20 15:14:21.403 myproject[3467:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteMutableData getCharacters:range:]: unrecognized selector sent to instance 0x1fa630'
2009-03-20 15:14:21.412 myproject[3467:20b] Stack: (
808221155,
806100816,
808224837,
807957033,
807851552,
807660389,
807733601,
807733297,
807891629,
812155873,
812293801,
18081,
817945012,
821160740,
821157652,
821149552,
807852041,
812070519,
807836299,
807834407,
827752032,
816118388,
816157144,
8381,
8244
)
terminate called after throwing an instance of 'NSException'
(gdb) info symbol 18081
-[InternalChoicesController imagePickerController:didFinishPickingImage:editingInfo:] + 73 in section LC_SEGMENT.__TEXT.__text of /Users/isaacwaller/Documents/myproject/build/Debug-iphoneos/myproject.app/myproject
(gdb) info symbol 812293801
NSLog + 25 in section LC_SEGMENT.__TEXT.__text of /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Foundation

所以我认为编码Base64数据有问题吗?我正在使用的代码是:

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

@implementation NSData (Base64)

- (NSString *)encodeBase64;
{
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

谢谢,艾萨克·沃勒

4

2 回答 2

4

我会检查代码是否有错别字。该错误非常清楚地表明 NSMutableData 的实例正在使用 NSString 预期。似乎 encodeBase64 以某种方式返回self,或者原始 UIImagePNGRepresentation(img) 被传递给stringByAppendingString:.

如果不明显,只需将其分解并在每个步骤中检查。(我在这个例子中使用了 NSLog,但是使用调试器也可以正常工作,当然。)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    NSData *rep = UIImagePNGRepresentation(img);
    NSLog(@"Rep: %@", rep);
    NSString *base64 = [rep encodeBase64];
    NSLog(@"Base 64 is a %@", NSStringFromClass([base64 class]));
    self.somestring = [@"hardcodedstring" stringByAppendingString:base64]; //encodeBase64 encodes it to base64

}

我最好的猜测是你不小心传入了原始的 NSData 而不是 NSString ——但是如果失败了,上面的代码应该可以正常工作或者准确地显示出问题的地方。

于 2009-03-21T00:57:04.233 回答
2

@"Some String"不是一个对象,它是一个字符串文字。您无法向其发送消息。您将需要做其他事情来加入这些字符串。

就像是:

 [NSString stringWithFormat:@"%@%@", @"String 1", @"String 2"];

显然,这是不正确的。字符串文字被视为对象。

如评论中所述,您也可能有问题self.somestring。如果您没有声明属性或合成somestring,那么通过它访问它self.是不正确的。你应该只使用

somestring = [NSString stringWithFormat:@"%@%@", @"String 1", @"String 2"];

如果您已经完成:

@interface myClass {
  NSString *somestring;
}
@property(nonatomic, retain) NSString *somestring;
@end

和:

@implementation myClass
@synthesize somestring;
@end

然后您可以使用 访问变量self.somestring,这实际上只是 . 的语法糖[self somestring]

同样重要的是要注意,@property并且@synthesize实际上是语法糖位本身。他们最终会做类似的事情。

@interface myClass {
  NSString *somestring;
}

-(NSString *)somestring;
-(void)setSomestring:(NSString *)value;
@end

和:

@implentation myClass
-(NSString *)somestring {
  return somestring;
}
-(void)setSomestring:(NSString *)value {
  somestring = value;
}
@end

因此,如果您尚未将其声明somestring为属性并对其进行合成,那么您就没有这些方法来回答正在传递的消息。

于 2009-03-20T21:31:29.827 回答