Programmatically display iPhone / iPad UDID

I have an iPad application in which I get the UDID of the device using the following code in the viewDidLoad method.

uid = [[UIDevice currentDevice] uniqueIdentifier]; uid = [uid stringByReplacingOccurrencesOfString:@"-" withString:@""]; 

I need to remove a dash in a line.

Later, in another method call, I try to access this uid string (which is a property of this class),

 NSLog("Loading request with UDID: %@", uid); 

and I get the following in the console window when I try to print it.

 Loading request with UDID: ( <WebView: 0x4b0fb90> ) 

Why does it print the memory address and not the string itself? Thanks!

+4
source share
3 answers

The problem you are facing is memory management. I used to have this problem.

When you are an NSLog uid, what you get is the address for the WebView object. Why does this happen when the uid is NSString ??? Let me get to know the magic of memory management: D

When you set your uid variable in this line:

 uid = [uid stringByReplacingOccurrencesOfString:@"-" withString:@""]; 

What you did is set the autoreleased variable to uid. This means that it will be released, and this place of memory will be for captures. Between this function, it ends the next time you access it, it was released and something else was stored there.

How do you fix this? When you assign something to a property, such as uid, ALWAYS execute it using the setter methods created in the @property declaration. Use either self.uid = string or [self setUid:string . This will allow you to correctly release the old line and save the new one.

This is a problem that cost me many hours trying to find a problem line. Another symptom that can happen is a program crash when trying to send a message to a released object. They can be VERY hard to track. I hope my answer helps you and you don’t have to endure this frustration :)

Good luck

+10
source

NSLog ("...") must be NSLog (@ "...")

+3
source

uniqueIdentifier been disabled from iOS 5.

You should use another function:

 [[[UIDevice currentDevice] identifierForVendor] UUIDString] 
+2
source

All Articles