Ios NSAttributedString和NSMuttableAttributedString出现问题
我有以下代码,不断崩溃,无法找到原因Ios NSAttributedString和NSMuttableAttributedString出现问题,ios,nsattributedstring,cs193p,Ios,Nsattributedstring,Cs193p,我有以下代码,不断崩溃,无法找到原因 @property (nonatomic)NSUInteger numberOfShapes; @property (strong, nonatomic)NSString *shapeInCard; @property (strong, nonatomic)UIColor *colorOfShape; @property (nonatomic)float shadeOfColor;enter code here - (NSAttributedString
@property (nonatomic)NSUInteger numberOfShapes;
@property (strong, nonatomic)NSString *shapeInCard;
@property (strong, nonatomic)UIColor *colorOfShape;
@property (nonatomic)float shadeOfColor;enter code here
- (NSAttributedString *) contents
{
NSMutableAttributedString *cardContents = [[NSMutableAttributedString alloc] initWithString:@""];
UIColor *shapeColor = [self.colorOfShape colorWithAlphaComponent:self.shadeOfColor];
NSAttributedString * shapeToUse = [[NSAttributedString alloc] initWithString:self.shapeInCard attributes:@{NSBackgroundColorAttributeName:shapeColor}]; <-------
for (int i=0; i<self.numberOfShapes; i++) {
[cardContents appendAttributedString:shapeToUse]; <------
if(i < self.numberOfShapes-1) {
[cardContents appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
}
}
return cardContents;
}
@property(非原子)整数numberOfShapes;
@属性(强,非原子)NSString*shapeInCard;
@性质(强,非原子)UIColor*形状的颜色;
@属性(非原子)float shadeOfColor;在这里输入代码
-(NSAttributedString*)内容
{
NSMUTABLeAttributeString*cardContents=[[NSMUTABLeAttributeString alloc]initWithString:@'';
UIColor*shapeColor=[self.colorOfShape colorWithAlphaComponent:self.shadeOfColor];
NSAttributedString*shapeToUse=[[NSAttributedString alloc]initWithString:self.shapeInCard属性:@{NSBackgroundColorAttributeName:shapeColor}];self.shapeInCard
是NSString
还是NSNumber
?确保它是NSString
,如果不是,那可能是问题self的类型。shapeInCardself.shapeInCard不是字符串或没有长度方法。您应该用nslog检查self.shapeInCard。问题似乎在e你初始化了你的属性。你能把代码添加到你要做的地方吗?@Sunny shah谢谢你的回答。虽然self.shapeInCard在很久以前就被声明为一个NSString,但我做了NSlog,发现它也在那里崩溃了…所以,那里出了点问题…我今天晚些时候会继续检查。谢谢大家的建议手势。