Tony Xu
Tony Xu

Reputation: 3091

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

My question may be naive, I guess, since object description is usually used for debug and output in NSLog. I also have to admit that my approach of attempting use object.description may be wrong. However, I did find in my case, information in description is just what I need. If I can fetch out the part easily from the description.

Okay, here is my code and what I need is user first name (not username):

    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 { //.....
        }

account description gives out:

     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: [email protected]

check out this part: properties: { fullname = "LOOKAT HERE"; uid = 100004223213342323; }

This is exactly what I need. If I use this, I don't have to do SLRequest and SLRequestMethodGET etc to get user name (which I didn't figure out yet, another motivation why I am attempted to use object description).

My question is whether it's wrong to parse description and get out fullname property? If wrong, why? Following question is: Wrong or not, how can I get fullname property of description gracefully (such as convert description to something that uses dot and dot to get property, not to chop the string and get that fullname part), because account.description.fullname or account.fullname so doesn't work although account.username works.

Thanks for your time/input.

Upvotes: 1

Views: 774

Answers (2)

AddisDev
AddisDev

Reputation: 1801

This is not the most efficient way but it works well for me:

ACAccount *acc = theAccount;
NSArray *parse = [acc.description componentsSeparatedByString:@"uid = "];
parse = [[parse objectAtIndex:1] componentsSeparatedByString:@";"];
NSString *UID = [parse objectAtIndex:0];
parse = [acc.description componentsSeparatedByString:@"fullname = \""];
parse = [[parse objectAtIndex:1] componentsSeparatedByString:@"\";"];
NSString *name = [parse objectAtIndex:0];

Upvotes: 0

Tommy
Tommy

Reputation: 100632

It is wrong to parse the description. It's wrong because the description has no documented format. It may change arbitrarily from one instance of an object to the next and from one version of the OS to the next. So your code is likely to break. By relying on undocumented behaviour you're also in breach of your iOS Developer Programme agreement with Apple, giving them the right to pull your apps at any time.

Furthermore if for some reason you're absolute desperate to use undocumented API then you might as well use [self.facebookAccount.description valueForKey:@"fullName"]; it'll be just as robust to OS changes without you trying to second guess how Core Data object descriptions are output.

Upvotes: 3

Related Questions