gpt4 book ai didi

objective-c - 将 Twitter 帐户添加到 ABRecordRef(人员)的最简单方法

转载 作者:行者123 更新时间:2023-11-28 18:13:05 27 4
gpt4 key购买 nike

所以我需要将一个 Twitter 帐户添加到 ABRecordRef 中。然而,最快的方法似乎是获取社交资料属性的多值引用,创建它的可变版本,查找它是否有 Twitter 条目,如果已​​经有,则创建可变副本字典并用新字典替换它。否则,使用 twitter 键和用户名创建一个新的字典条目。

实际上它非常冗长:

   ABRecordRef person;

ABMultiValueRef oldsocials = ABRecordCopyValue(abperson, kABPersonSocialProfileProperty);
ABMutableMultiValueRef newsocials;
if(oldsocials){
newsocials = ABMultiValueCreateMutableCopy(oldsocials);
} else {
newsocials = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
}

CFIndex socialsCount = ABMultiValueGetCount(socials);

bool foundtwitter = false;

for (int k=0 ; k<socialsCount ; k++) {
CFDictionaryRef socialValue = (CFDictionaryRef)ABMultiValueCopyValueAtIndex(socials, k);
if(socialValue){
if(CFStringCompare( (CFStringRef)CFDictionaryGetValue(socialValue, kABPersonSocialProfileServiceKey), kABPersonSocialProfileServiceTwitter, 0)==kCFCompareEqualTo) {

CFMutableDictionaryRef tomutate = CFDictionaryCreateMutableCopy(socialValue);

CFDictionarySetValue(tomutate, kABPersonSocialProfileUsernameKey, toNS(thetwitter));

ABMultiValueReplaceValueAtIndex(socials, tomutate, k);

CFRelease(tomutate);

foundtwitter = true;
}

CFRelease(socialValue);
}

if(foundtwitter)
break;

}


if(!foundtwitter){
ABMultiValueAddValueAndLabel(newsocials, [NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kABPersonSocialProfileServiceTwitter, kABPersonSocialProfileServiceKey,
@"justinbieber", kABPersonSocialProfileUsernameKey,
nil], kABPersonSocialProfileServiceTwitter, NULL);
}


ABRecordSetValue(abperson, kABPersonSocialProfileProperty, newsocials, NULL);

if(oldsocials)
CFRelease(oldsocials);

CFRelease(newsocials);

加twitter其实挺长的,感觉自己哪里做错了。我的意思是,理想情况下,在另一种语言上添加类似的东西将接近于此:

person["socialServices"]["twitter"] = "@SomeUserName";
person.save();

我是不是漏掉了什么?

最佳答案

看看下面我的 friend ,对于 facebook 和 twitter - 它会起作用:

 //adding social
ABMultiValueRef social = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
ABMultiValueAddValueAndLabel(social, (__bridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kABPersonSocialProfileServiceTwitter, kABPersonSocialProfileServiceKey,
[tempVCard objectForKey:@"twitterUserName"], kABPersonSocialProfileUsernameKey,
nil]), kABPersonSocialProfileServiceTwitter, NULL);
ABMultiValueAddValueAndLabel(social, (__bridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kABPersonSocialProfileServiceFacebook, kABPersonSocialProfileServiceKey,
[tempVCard objectForKey:@"facebookUserName"], kABPersonSocialProfileUsernameKey,
nil]), kABPersonSocialProfileServiceFacebook, NULL);

ABRecordSetValue(person, kABPersonSocialProfileProperty, social, NULL);
CFRelease(social);

我的私有(private)数组“tempVCard”将是您的用户动态数据数组。

关于objective-c - 将 Twitter 帐户添加到 ABRecordRef(人员)的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12290588/

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