我正在尝试使用CNContact获取我的联系人的电话号码,我希望将该号码作为其简单的字符串,例如"04xxxxxxxx"
,但我能得到的最接近的是以下内容。 ("联系"属于CNContact类型)
contact.phoneNumbers[0].value
\\Which prints: <CNPhoneNumber: 0x13560a550: countryCode=au, digits=04xxxxxxxx>
我尝试了所有明显的事情而不是那么明显的事情,谢谢
答案 0 :(得分:2)
如果有人有更合法的解决方案,请发布,否则这种相当黑客的方法有效:
let value = String(contact.phoneNumbers[0].value)
let start = value.rangeOfString("digits=")!.endIndex
let end = value.endIndex.predecessor()
let number = value.substringWithRange(Range<String.Index>(start: start, end: end))
答案 1 :(得分:0)
使用此类别,您现在可以通过swift访问电话号码。 不要忘记将此文件包含在桥接标题中
typedef struct {
char *name;
char *surname;
char *UUN;
char *department;
char gender;
int age;
} student_t;
int main(void) {
int i, len;
student_t student_t[6];
student_t[0].name = "John";
student_t[0].surname = "Bishop";
student_t[0].UUN = "s1234";
student_t[0].department = "Inf";
student_t[0].gender = 'm';
student_t[0].age = 18;
student_t[1].name = "Lady";
student_t[1].surname = "Cook";
student_t[1].UUN = "s2345";
student_t[1].department = "Eng";
student_t[1].gender = 'f';
student_t[1].age = 21;
student_t[2].name = "James";
student_t[2].surname = "Jackson";
student_t[2].UUN = "s3456";
student_t[2].department = "Eng";
student_t[2].gender = 'm';
student_t[2].age = 17;
for (i = 3; i < 6; i++) {
printf("Enter your name: ");
scanf(" %s", student_t[i].name);
printf("Enter your surname: ");
scanf(" %s", student_t[i].surname);
printf("Enter your UUN: ");
scanf(" %s", student_t[i].UUN);
printf("Enter your department: ");
scanf(" %s", student_t[i].department);
printf("Enter your gender: ");
scanf(" %c", &student_t[i].gender);
printf("Enter your age: ");
scanf(" %d", &student_t[i].age);
}
return EXIT_SUCCESS;
}
答案 2 :(得分:0)
获取数字值,如下所示:
let number = value.valueForKey("digits") as! String
从iOS9开始:
let number = value.stringValue
根据Apple关于CNPhoneNumber的文件:
stringValue的 属性 电话号码的字符串值。 (只读)
宣言 迅速 var stringValue:String {get}