ios - Error in NSDecimalNumberDivision: 'NSInvalidArgumentException', reason: '-[__NSCFNumber decimalNumberByDividingBy:]: unrecognized selector? -


my code

//    nsdecimalnumber *percentspent = [distributionmodel.spent decimalnumberbydividingby:self.monthlysummarymodel.totalspent]; //    nslog(@"percent:%@", percentspent);     nslog(@"spent:%@, totalspent:%@", distributionmodel.spent, self.monthlysummarymodel.totalspent); 

where

@property (nonatomic, strong) nsdecimalnumber *spent; @property (nonatomic) nsdecimalnumber *totalspent; 

console log when percentspent commented out

2014-12-01 15:38:20.161 app-ios[15980:60b] spent:27.01, totalspent:2077.01  2014-12-01 15:38:20.201 app-ios[15980:60b] spent:2000, totalspent:2077.01  2014-12-01 15:38:20.251 app-ios[15980:60b] spent:40, totalspent:2077.01  2014-12-01 15:38:20.292 app-ios[15980:60b] spent:10, totalspent:2077.01  

error when execute calculate percentspent

2014-12-01 15:35:44.843 app-ios[15963:60b] -[__nscfnumber decimalnumberbydividingby:]: unrecognized selector sent instance 0x17d4f020  2014-12-01 15:35:44.851 app-ios[15963:60b] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[__nscfnumber decimalnumberbydividingby:]: unrecognized selector sent instance 0x17d4f020'  *** first throw call stack:  (0x2eaadf83 0x3925eccf 0x2eab1917 0x2eab0203 0x2e9ff768 0xac64b 0x313fd8f7 0x313a4c27 0x313a447d 0x312cad59 0x30f4862b 0x30f43e3b 0x30f43ccd 0x30f436df 0x30f434ef 0x30f3d21d 0x2ea79255 0x2ea76bf9 0x2ea76f3b 0x2e9e1ebf 0x2e9e1ca3 0x338e7663 0x3132e14d 0xa48cd 0x3976bab7)  libc++abi.dylib: terminating uncaught exception of type nsexception  signal: 6 (signal sigabrt) 

update

@interface monthlysummarymodel : nsobject @property (nonatomic) int year; @property (nonatomic) int month; @property (nonatomic) nsdecimalnumber *totalspent; @property (nonatomic, strong) nsarray *distributions; 

and

@interface monthlysummarydistributionmodel : nsobject @property (nonatomic, strong) nsstring *group; @property (nonatomic, strong) nsdecimalnumber *spent;  - (monthlysummarydistributionmodel *) initwithjson: (nsdictionary *) json; @end 

what issue here?

a short , delightful essay on causes dreaded "unrecognized selector" crash

you have crashed because have sent nsdecimalnumber message not nsdecimalnumber - nsnumber. may think nsdecimalnumber, , think that, comment shows:

but made spent nsdecimalnumber

... you wrong. believe runtime, not gut. runtime knows better (clearly) sort of object is!

why possible? how did ever situation? explain.

objective-c polymorphic. means doesn't matter how declare thing; matters is. , can different! reason can turn off type checking (accidentally) , assign object of totally wrong type variable.

for example (hold beer , watch this):

nsstring* s; nsnumber* n = [nsnumber new]; nsarray* arr = @[n]; s = arr[0]; 

think i've done. put nsnumber array, pulled out again, , assigned nsstring variable! , compiler didn't stop me!!!!! that's because pulling out of nsarray turns off compiler's type-checking on thing - can assign anything, because compiler has no idea type of thing is.

but still has type in real life. so, when program runs, , when send nsstring message s after that, crash, because not nsstring, despite declaration.

that example of accidentally hiding something's type compiler. sometimes, people worse; deliberately hide something's type compiler. example (hold beer again):

nsstring* s; nsnumber* n = [nsnumber new]; s = (nsstring*) n; 

whoa! time straight out lied compiler , told nsnumber nsstring!!!!! , compiler shrugged , said: "ok, whatever. guess knows don't know." let me that, , let me assign nsnumber nsstring variable.

but once again, still nsnumber. when try treat nsstring, crash @ runtime.

the moral of that story is: don't lie compiler! let it judge of kind of thing thing is, wherever possible.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -