Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2010, 04:49 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 100
mindus is on a distinguished road
Default NSNumber sum

Hi

NSNumber start;
start = [NSNumber numberWithInt:([start intValue] + 5)];

i can able to increment up to three times (start = 16). After that if i try to increase this, i am getting Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UILabel intValue]: unrecognized selector sent to instance 0x392be00'
exception

anybody know how to solve this

Thanks
mindus
mindus is offline   Reply With Quote
Old 08-09-2010, 04:56 AM   #2 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: Amsterdam, The Netherlands
Posts: 782
TUX2K is on a distinguished road
Default

It looks like that some how your object changes from NSNumber to UILabel.
Could you post a bit more code?
TUX2K is offline   Reply With Quote
Old 08-09-2010, 06:39 AM   #3 (permalink)
Registered Member
 
Ice_2k's Avatar
 
Join Date: Apr 2010
Location: Bucharest, Romania
Posts: 148
Ice_2k is on a distinguished road
Default

try using NSNumber*
Ice_2k is offline   Reply With Quote
Old 08-09-2010, 09:26 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

numberWithInt: returns an autoreleased NSNumber object. If you don't explicitly retain it, it will be deallocated when the autorelease pool is drained at the end of the current iteration of the run loop. When this happens, 'start' becomes a dangling pointer -- the object it points to no longer exists.

When an object is deallocated, the memory isn't necessarily replaced right away, so it's possible that subsequent messages sent will still work normally for a while. Eventually, though, the freed memory is reused for a new object -- in your case, the memory previously occupied by the NSNumber now contains a UILabel. The pointer 'start' now erroneously points to a UILabel, which doesn't know how to respond to the intValue message and throws the exception you're seeing.

So, you need to make sure to retain the NSNumber returned by the numberWithInt: method, and then release it once you no longer need it. The easiest way to do this is by creating and using a retained property; this will take care of the memory management for you. See Brian Slick's properties guide for an overview.
ChrisL is offline   Reply With Quote
Old 08-09-2010, 10:47 AM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by mindus View Post
Hi

NSNumber start;
start = [NSNumber numberWithInt[start intValue] + 5)];

i can able to increment up to three times (start = 16). After that if i try to increase this, i am getting Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UILabel intValue]: unrecognized selector sent to instance 0x392be00'
exception

anybody know how to solve this

Thanks
mindus
The other poster told you what was wrong with your code. You need to retain the NSNumber you are creating, and release the old one before you replace it.

This would be a perfect application for using a retained property.

If you declare start as a retained property in your header, like this:

Code:
@property (nonatomic, retain) NSNumber* start;
and in the body of your class, synthesize it like this:

Code:
@synthesize start;
Then your code can read like this:

Code:
self.start = [NSNumber numberWithInt:([start intValue] + 5)];
Note that we are saving a value to start using property notation (self.start). That causes the code to call the setter for the start property, which retains the new value and releases the old value.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 341
11 members and 330 guests
arash5500, HemiMG, iram91419, linkmx, mediaspree, nadav@webtview.com, Objective Zero, Paul Slocum, stanny, Touchmint, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:37 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0