ios - [__NSArrayI length]: unrecognized selector sent to instance when moving through views -
this question has answer here:
i have encountered error when click on label @ master view leads detail view (which should show labels displayed in data model @ reasonlibrary.
my code:
master view controller header
@interface masterviewcontroller : uiviewcontroller @property (strong, nonatomic) iboutletcollection(uilabel) nsarray *reasonlabelviews; @end
master view controller implementation
#import "masterviewcontroller.h" #import "detailviewcontroller.h" #import "reasons.h" @interface masterviewcontroller () @end @implementation masterviewcontroller - (void)viewdidload { [super viewdidload]; (nsuinteger index = 0; index < self.reasonlabelviews.count; index++) { reasons *reason = [[reasons alloc] initwithindex:index]; uilabel *reasonlabelview = self.reasonlabelviews[index]; reasonlabelview.text = reason.reasonsrazao; } } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } -(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { uilabel *reasonlabelview = (uilabel *)[sender view]; if ([self.reasonlabelviews containsobject:reasonlabelview]) { nsuinteger index = [self.reasonlabelviews indexofobject:reasonlabelview]; detailviewcontroller *detailviewcontroller = (detailviewcontroller *)segue.destinationviewcontroller; detailviewcontroller.reason = [[reasons alloc] initwithindex:index]; } } - (ibaction)showreasondetail:(id)sender { [self performseguewithidentifier:@"showreasondetail" sender:sender]; } @end
detail view controller header
#import <uikit/uikit.h> @class reasons; @interface detailviewcontroller : uiviewcontroller @property (strong, nonatomic) reasons *reason; @property (weak, nonatomic) iboutlet uilabel *reasonlabel; @property (weak, nonatomic) iboutlet uilabel *motivelabel; @property (weak, nonatomic) iboutlet uilabel *zerofivelabel; @end
detail view controller implementation
#import "detailviewcontroller.h" #import "reasons.h" @interface detailviewcontroller () @end @implementation detailviewcontroller - (void)viewdidload { [super viewdidload]; if (self.reason) { self.reasonlabel.text = self.reason.reasonsrazao; self.motivelabel.text = self.reason.randomfact; self.zerofivelabel.text = self.reason.reasonsdezeroacinco; } } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
reasonslibrary header
#import <foundation/foundation.h> extern nsstring *const krazao; extern nsstring *const kmotivo; extern nsstring *const kdezeroacinco; @interface reasonlibrary : nsobject @property (strong,nonatomic) nsarray *library; - (nsstring *)randomfact; @end
reasonslibrary implementation
#import "reasonlibrary.h" @implementation reasonlibrary nsstring *const krazao = @"razao"; nsstring *const kmotivo = @"motivo"; nsstring *const kdezeroacinco = @"dezeroacinco"; - (instancetype)init { self = [super init]; if (self) { _library = @[@{krazao: @"razao 1", kmotivo: @[@"por que sim",@"por que demais",@"por que muito",@"por que foda",@"por que top",@"por que demasiado"], kdezeroacinco: @"1", }, @{krazao: @"razao 2", kmotivo: @[@"por que sim",@"por que demais",@"por que muito",@"por que foda",@"por que top",@"por que demasiado"], kdezeroacinco: @"2", }, @{krazao: @"razao 3", kmotivo: @[@"por que sim",@"por que demais",@"por que muito",@"por que foda",@"por que top",@"por que demasiado"], kdezeroacinco: @"3", }, @{krazao: @"razao 4", kmotivo: @[@"por que sim",@"por que demais",@"por que muito",@"por que foda",@"por que top",@"por que demasiado"], kdezeroacinco: @"4", }, @{krazao: @"razao 5", kmotivo: @[@"por que sim",@"por que demais",@"por que muito",@"por que foda",@"por que top",@"por que demasiado"], kdezeroacinco: @"5", }, @{krazao: @"razao 6", kmotivo: @[@"por que sim",@"por que demais",@"por que muito",@"por que foda",@"por que top",@"por que demasiado"], kdezeroacinco: @"6", } ]; } return self; } - (nsstring *)randomfact { int random = arc4random_uniform((int)self.library.count); return [self.library objectatindex:random];
} @end
reasons header
#import <foundation/foundation.h> @interface reasons : nsobject @property (strong, nonatomic) nsstring *reasonsrazao; @property (strong, nonatomic) nsstring *reasonsmotivo; @property (strong, nonatomic) nsstring *reasonsdezeroacinco; @property (strong, nonatomic) nsstring *randomfact; -(instancetype)initwithindex:(nsuinteger)index; @end
reasons implementation
#import "reasons.h" #import "reasonlibrary.h" @implementation reasons -(instancetype)initwithindex:(nsuinteger)index { self = [super init]; if (self) { reasonlibrary *reasonlibrary = [[reasonlibrary alloc] init]; nsarray *library = reasonlibrary.library; nsdictionary *reasonsdictionary = library[index]; _reasonsrazao = [reasonsdictionary objectforkey:krazao]; _reasonsmotivo = [reasonsdictionary objectforkey:kmotivo]; _reasonsdezeroacinco = [ reasonsdictionary objectforkey:kdezeroacinco]; } return self; } @end
object key kmotivo
nsarray
in code. assigning nsstring
property , afterward uilabel text property. uilabel
tries call length on nsarray
, crash exception.
something following work:
nsarray *motives = [reasonsdictionary objectforkey:kmotivo]; if (motives != nil && motives.count > 0) { int random = arc4random_uniform(motives.count); _reasonsmotivo = motives[random]; }
code above take first available motive array , assign _reasonsmotivo
. should inserted instead of following code line:
_reasonsmotivo = [reasonsdictionary objectforkey:kmotivo];
Comments
Post a Comment