gpt4 book ai didi

objective-c - iOS Objective-C : how can I get useful information out of object description?

转载 作者:行者123 更新时间:2023-12-01 19:16:57 25 4
gpt4 key购买 nike

我想我的问题可能很幼稚,因为对象描述通常用于 NSLog 中的调试和输出。我也不得不承认我尝试使用 object.description 的方法可能是错误的。但是,我确实发现,描述中的信息正是我所需要的。如果我可以从描述中轻松取出零件。

好的,这是我的代码,我需要的是用户名(不是用户名):

    self.facebookAccountStore = [[ACAccountStore alloc] init];

ACAccountType* facebookAccountType = [self.facebookAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
//[self.facebookAccountStore requestAccessToAccountsWithType:facebookAccountType withCompletionHandler:^(BOOL granted, NSError* e) {
[self.facebookAccountStore requestAccessToAccountsWithType:facebookAccountType options:facebookOptions completion:^(BOOL granted, NSError* e) {
if (granted) {
NSArray* accounts = [self.facebookAccountStore accountsWithAccountType:facebookAccountType];
self.facebookAccount = [accounts lastObject];
NSLog(@"acct description: %@", self.facebookAccount.description);

NSLog(@"acct type: %@", self.facebookAccount.accountType);
NSLog(@"acct credential: %@", self.facebookAccount.credential);
NSLog(@"acct identifier: %@", self.facebookAccount.identifier);
NSLog(@"acct username: %@", self.facebookAccount.username);
} else { //.....
}

帐户说明给出:
     acct description: 
objectID: x-coredata://F8123001-FB33-48D4-B1A7-EXXXX1243XXXX/Account/p10
enabledDataclasses: {(
"com.apple.Dataclass.Contacts",
"com.apple.Dataclass.Calendars"
)}
enableAndSyncableDataclasses: {(
)}
properties: {
fullname = "LOOKAT HERE";
uid = 100004223213342323;
}
parentAccount: (null)
owningBundleID:(null)


type:com.apple.facebook
identifier: EF34399A-8577-459B-BE5E-FD12132SEDSFE
accountDescription: Facebook
username: pspped.ok@mailserver.com

看看这部分:
特性: {
全名 = "看这里";
uid = 100004223213342323;
}

这正是我所需要的。如果我使用它,我不必执行 SLRequest 和 SLRequestMethodGET 等来获取用户名(我还没有弄清楚,这是我尝试使用对象描述的另一个动机)。

我的问题是解析描述并获取全名属性是否错误?如果错了,为什么?
以下问题是:错误与否,我怎样才能优雅地获取描述的全名属性(例如将描述转换为使用点和点来获取属性的东西,而不是截断字符串并获取全名部分),因为 account.description。 fullname 或 account.fullname 所以虽然 account.username 有效,但它不起作用。

感谢您的时间/投入。

最佳答案

解析 description 是错误的.这是错误的,因为描述没有记录格式。它可以从一个对象实例任意更改为下一个实例,也可以从一个操作系统版本任意更改为下一个版本。所以你的代码很可能会出错。依赖未记录的行为,您也违反了您与 Apple 签订的 iOS 开发者计划协议(protocol),该协议(protocol)赋予他们随时拉取您的应用程序的权利。

此外,如果出于某种原因您绝对不顾一切地使用未记录的 API,那么您不妨使用 [self.facebookAccount.description valueForKey:@"fullName"] ;它对操作系统的变化同样健壮,无需您再次猜测 Core Data 对象描述是如何输出的。

关于objective-c - iOS Objective-C : how can I get useful information out of object description?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12716946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com