Terminating app due to uncaught exception ' nsunknownkeyexception ', Reason: ' [<datamodel 0x7fe44b

Source: Internet
Author: User

Self.model=[[datamodel Alloc]init];
[Self.model setvalue:@ "Searph" forkey:@ "StockName"];
[Self.model setvalue:@ "10.0" forkey:@ "Price"];
[Self.model addobserver:self forkeypath:@ "Price" options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil];


Self.mylabel=[[uilabel alloc]initwithframe:cgrectmake (100, 100, 100, 30)];
Self.mylabel.layer.maskstobounds=yes;
self.mylabel.layer.cornerradius=50;
Self.mylabel.textcolor=[uicolor Cyancolor];
Self.mylabel.text=[self.model valueforkey:@ "Price"];
[Self.view AddSubview:self.myLabel];

Self.mybutton=[uibutton Buttonwithtype:uibuttontyperoundedrect];
Self.mybutton.frame=cgrectmake (200,200, 100, 30);
[Self.mybutton addtarget:self Action: @selector (ChangeValue) forcontrolevents:uicontroleventtouchupinside];
[Self.mybutton Setbackgroundcolor:[uicolor Lightgraycolor];
[Self.view AddSubview:self.myButton];
}
-(void) changevalue{
[Self.model setvalue:@ "20.0" forkey:@ "price"];
}
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{
if ([KeyPath isequaltostring:@ "Price"]) {
Self.mylabel.text=[self.model valueforkey:@ "Price"];
}
}

Error when the program is running

Terminating app due to uncaught exception ' nsunknownkeyexception ', Reason: ' [<datamodel 0x7fe44bf22810> Setvalu E:forundefinedkey:]: This class was not key value coding-compliant for the key price. '

After inspection, Self.mylabel.text=[self.model valueforkey:@ "Price" was found, self.mylabel.text=[nsstring stringwithformat:@ "%@" was changed, [ Self.model valueforkey:@ "Price"];

Terminating app due to uncaught exception ' nsunknownkeyexception ', Reason: ' [<datamodel 0x7fe44b

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.