Objective c IOS NSString属性在引用类中始终为零
这是我的@interface defObjective c IOS NSString属性在引用类中始终为零,objective-c,ios,nsstring,alassetslibrary,Objective C,Ios,Nsstring,Alassetslibrary,这是我的@interface def @interface Thumbnail : UIView <NSCoding>{ NSMutableString *imageCacheKeyBase; } @property (nonatomic, copy) NSMutableString *imageCacheKeyBase; @end 然后在缩略图类中名为initWithAsset的方法中: self.urlString = [[_asset.defaultRepresen
@interface Thumbnail : UIView <NSCoding>{
NSMutableString *imageCacheKeyBase;
}
@property (nonatomic, copy) NSMutableString *imageCacheKeyBase;
@end
然后在缩略图类中名为initWithAsset的方法中:
self.urlString = [[_asset.defaultRepresentation.url absoluteString] copy];
c = (char*)[urlString UTF8String];
while (*c != '=')
++c;
++c;
self.imageCacheKeyBase = [NSString stringWithFormat:@"%s_", c];
因此,当Thumbnail allocing类尝试引用Thumbnail.imageCacheKeyBase时,Stumbin'值为零。我已经尝试了一百万种不同的方法来获取字符串变量作为缩略图的属性。我甚至试过简单的self.imageCacheKeyBase=@“dave”。努廷。我尝试过保留和保留保留(我知道这很愚蠢,但我什么都尝试过。我甚至尝试过简单地将属性设置为char*)
我整天都在研究这个
请提供帮助。在下面的某个地方,某个值为零。请尝试记录每个对象或数据类型的值
NSLog(@"My first string is %@, the char is %s, the final string is %@", [_asset.defaultRepresentation.url absoluteString], c, self.imageCacheKeyBase);
看起来您正在尝试在输入URL中添加所有内容,包括
=
。我建议您不要转换为UTF-8字符串(除非您有充分的理由这么做,但您没有提到),而是执行以下操作:
self.urlString = [[_asset.defaultRepresentation.url absoluteString] copy];
NSRange range = [self.urlString rangeOfString@"="];
if (range.location != NSNotFound)
{
self.imageCacheKeyBase = [NSString stringWithFormat:@"%s=", [self.urlString subStringToIndex:range.location];
}
我还要在.h中声明imageCacheKeyBase
,如下所示:
@property (nonatomic, retain) NSString *imageCacheKeyBase;
除非您需要它是可变的。如果您确实需要它是可变的,那么在我上面的代码片段中,将[NSString stringWithFormat…
更改为[NSMutableString stringWithFormat…
。因为您在这个类中创建字符串,您只需要保留,这比复制更可取
此外,您在代码段中引用了self.urlString
,但它没有在@界面中声明。可能您只是想在生成imageCacheKeyBase时将其作为临时局部变量?如果是,请将上面代码段中的第一行更改为:
NSString* urlString = [_asset.defaultRepresentation.url absoluteString];
absoluteString
返回一个自动释放字符串,因此无需释放它。好吧,我上次检查,c字符串是一个字符*%s和字符*自从kernigan和richie发明c语言以来就像花生酱和果冻一样混在一起了。不过,我想我的帖子可能有点误导。c被声明为char*c;你们这些人你看到了其他问题,比如什么?
NSString* urlString = [_asset.defaultRepresentation.url absoluteString];