我有以下课程
#import <UIKit/UIKit.h>
@interface UIImageViewInfo : UIImageView
@property(nonatomic,strong) NSString* colorInfo;
@end
在function1中创建UIImageViewInfo时(下面的帖子)我得到了正确的结果
“UIImageViewInfo:0x1d8acd60; baseClass = UIImageView; frame =(0 0; 20 20); opaque = NO; userInteractionEnabled = NO; tag = 2000; layer = CALayer:0x1d8a0560&gt;&gt;“
但是在function2中创建了一些东西(在下面发布)我得到 - (null)
UIImageViewInfo:0x8372c40; baseClass = UIImageView; frame =(0 0; 20 20); alpha = 0; hidden = YES; opaque = NO; userInteractionEnabled = 没有; tag = 2000; layer = CALayer:0x8380090&gt;&gt; - (null)
为什么有 - (null)??
提前完成
P.S&GT;这里有更多来自我的代码
-(void)function1{
UIImageViewInfo *image;
self.solutionPinSlots = [[NSMutableArray alloc] init];
for (int i=0; i<M; i++) {
image = [[UIImageViewInfo alloc] initWithImage:[UIImage imageNamed:@"solutionPeg@2x.png"]];
image.translatesAutoresizingMaskIntoConstraints=NO;
image.hidden=YES;
image.alpha = 0;
image.colorInfo = @"clear";
image.tag = 2000*(self.brainController.slotsIndex+1) +i;
[self.solutionPinSlots addObject:image];
[self.view addSubview:(UIImageViewInfo *)self.solutionPinSlots[i]];
}
NSLog(@"solutionSlots: %@",self.solutionPinSlots);
__block int counter = 0;
[self.brainController.solution enumerateObjectsUsingBlock:^(NSString *peg, NSUInteger idx, BOOL *stop){
for (int i=0;i<M;i++) {
if ([self.brainController.slots[self.brainController.slotsIndex][i] isEqual:peg]) {
if (i==idx) {
((UIImageViewInfo *) self.solutionPinSlots[counter]).backgroundColor = [UIColor whiteColor];
((UIImageViewInfo *) self.solutionPinSlots[counter]).colorInfo=@"white";
}else{
((UIImageViewInfo *) self.solutionPinSlots[counter]).backgroundColor = [UIColor blackColor];
((UIImageViewInfo *) self.solutionPinSlots[counter]).colorInfo=@"black";}
((UIImageViewInfo *) self.solutionPinSlots[counter]).hidden=NO;
((UIImageViewInfo *) self.solutionPinSlots[counter++]).alpha=1;
}
}
}];
[self.solutionPinSlots shuffle];
[self updateSolutionPinSlots];
}
-(void)function2{
NSString *obj;
for (int idx=0; idx< M;idx++ ) {
UIImageViewInfo *image = [[UIImageViewInfo alloc] initWithImage:[UIImage imageNamed:@"solutionPeg@2x.png"]];
image.translatesAutoresizingMaskIntoConstraints=NO;
obj = (NSString *) [self.solutionPinSlots objectAtIndex:idx];
image.tag=2000*(self.brainController.slotsIndex+1)+idx;
image.hidden=NO;
image.alpha = 1;
if ([obj isEqualToString:@"clear"]) {
image.hidden=YES;
image.alpha = 0;
image.colorInfo=@"clear";
image.backgroundColor=[UIColor clearColor];
}else if ([obj isEqualToString:@"white"]){
image.colorInfo=@"white";
image.backgroundColor=[UIColor whiteColor];
}else if ([obj isEqualToString:@"black"]){
image.colorInfo=@"black";
image.backgroundColor=[UIColor blackColor];
}else{
NSLog(@"Something is Wrong!!!");
}
[self.solutionPinSlots replaceObjectAtIndex:idx withObject:image];
[self.view addSubview:image];
}
}
答案 0 :(得分:2)
没关系。它来自UIView的description
方法,它相当复杂并且会转储有关视图的大量信息。其中一个条件是您的配置被绊倒并最终导致(null)
。
安全无视。
UIKit中没有 UIImageViewInfo
类,因此实现必须在您的项目中。在该实现中,可能有description
方法实现如下:
- (NSString*)description {
....
return [NSString stringWithFormat:@"UIImageViewInfo:%p; %@ - %@", self, [super description], someInstanceVariableThatHappensToBeNil]];
}
只有你的description
可能更复杂,因为有时候这会输出。
当你对UIKit进行子类化时,不应该为 UI
的前缀添加前缀。
答案 1 :(得分:1)
这是因为你的NSLog调用(你没有显示)要求第二个对象,而第二个对象是null。