以编程方式显示 iPhone/iPad UDID

发布于 2024-09-11 12:40:28 字数 495 浏览 3 评论 0原文

我有一个 iPad 应用程序,通过 viewDidLoad 方法中的以下代码获取设备的 UDID。

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

我想删除字符串中的破折号。

后来,在另一个方法调用中,我尝试访问此 uid 字符串(这是此类的属性),

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

当我尝试打印它时,我在控制台窗口中看到以下内容。

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

为什么它打印内存地址,而不是字符串本身?谢谢!

I have an iPad app in which I get the device's UDID by the following code in the viewDidLoad method.

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

I wanted to remove the dashes in the string.

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 out.

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

Why is it printing the memory address, instead of the string itself? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫南 2024-09-18 12:40:28

您遇到的问题与内存管理有关。我以前也遇到过这个问题。

当您对 uid 进行 NSLog 时,您得到的是 WebView 对象的地址。当 uid 是 NSString 时为什么会发生这种情况???让我带您参观一下内存管理的魔力:D

当您在这一行设置 uid 变量时:

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

您所做的是将一个自动释放的变量分配给 uid。这意味着它将被释放并且该内存位置将可供争夺。在此函数结束和下次访问它之间,它已被释放,并且其他内容存储在那里。

你如何解决这个问题?当您将某些内容分配给像 uid 这样的属性时,始终通过 @property 声明创建的 setter 方法来完成。使用 self.uid = string[self setUid:string。这将正确释放旧字符串并保留新字符串。

这个问题让我花了很多时间试图找到有问题的线路。另一个可能发生的症状是当您尝试向已释放的对象发送消息时程序崩溃。这些可能很难追踪。希望我的回答对您有所帮助,您不必忍受这种挫败感:)

祝您好运!

The issue you're having has to do with memory management. I've had exactly this problem before.

When you NSLog the uid, what you get is the address for a WebView object. Why would that happen when uid is an NSString??? Let me take you on a guided tour of the magic of memory management :D

When you set your uid variable on this line:

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

What you did was assign an autoreleased variable to uid. That means it will be released and that memory location will be up for grabs. Between this function ending and the next time you access it, it has been released and something else was stored there.

How do you fix this? When you assign something to a property like uid, ALWAYS do it through the setter methods created by the @property declaration. Use either self.uid = string or [self setUid:string. This will properly release the old string and retain the new one.

This is a problem that has cost me MANY hours trying to find the problematic line. Another symptom that can happen is the program crashing when you try to send a message to a released object. These can be VERY hard to track down. Hopefully my reply helps you and you don't have to endure that frustration :)

Good luck!

飞烟轻若梦 2024-09-18 12:40:28

NSLog("...") 应该是 NSLog(@"...")

NSLog("...") should be NSLog(@"...")

祁梦 2024-09-18 12:40:28

uniqueIdentifier 已从 iOS 5 中弃用。

您应该使用其他函数:

[[[UIDevice currentDevice] identifierForVendor] UUIDString]

uniqueIdentifier was deprecapted from iOS 5.

You should use other function:

[[[UIDevice currentDevice] identifierForVendor] UUIDString]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文