雨燕2.0 Alamofire 2.0的Xcode 7的性能IOS 9如何知道是否追加功能,填補了NSObject的
我有一個調用API和檢索JSON格式的好友列表中的下一個函數,轉換列表在字典,並將其附加到友誼NSObject的
func GetFriends(completionHandler: ([FriendShip]?, NSError?) ->()) {
Alamofire.request(Router.GetFriends(Test().getUserId())).responseJSON { (_, _, result) in
var friends = [FriendShip]()
switch result {
case .Success(let json):
if let responseObject = json as? [String: AnyObject], let hits = responseObject["hits"] as? [[String: AnyObject]] {
for dictionary in hits {
friends.append(FriendShip(dictionary: dictionary))
print(friends)
}
completionHandler(friends, nil)
}
case .Failure(_, let error):
completionHandler(nil, error as NSError)
}
}
的打印(字典)結果是:
["_id": 546a6ef98e6df97032266, "friend": {
"_id" = 546a4b3e1f8d2c2630dd2;
name = "Daniela";
profileImageUrl = "https://api-static/profile/546a4b3e1f8d2c2630dd2.1.jpg";
statusTxt = "";
}]
["_id": 546a6f988e6df97032266, "friend": {
"_id" = 546a4ba51f8d2c2630dd2;
name = "Mara";
profileImageUrl = "https://api-static/profile/546a4ba51f8d2c2630dd2.1.jpg";
statusTxt = undefined;
}]
["_id": 546a70a18e6df97032266, "friend": {
"_id" = 546a4bd61f8d2c2630dd2;
name = "Alejandro";
profileImageUrl = "https://api-static/profile/546a4bd61f8d2c2630dd2.1.jpg";
statusTxt = "Marty";
}]
["_id": 546a715d8e6df97032266, "friend": {
"_id" = 546a4be01f8d2c2630dd2;
name = "Pedro";
profileImageUrl = "https://api-static/profile/546a4be01f8d2c2630dd1.1.jpg";
}]
類ES友誼和用戶
class FriendShip: NSObject{
var id: String?
var userId: String?
var user: User?
var friendId: String?
var friend: User?
var date: NSDate?
init(dictionary: [String: AnyObject]){
id = dictionary["id"] as? String
userId = dictionary["userId"] as? String
user = dictionary["user"] as? User
friendId = dictionary["friendId"] as? String
friend = dictionary["friend"] as? User
date = dictionary["date"] as? NSDate
}
override var description : String {
let friendString = friend!.name != nil ? friend!.name! : "nil"
let urlString = friend!.profileImageUrl != nil ? friend!.profileImageUrl! : "nil"
return "Friendship:\nfriend = \(friendString),\nurlString = \(urlString)"
}
}
class User: NSObject{
var id: String?
var name: String?
var statusTxt: String?
var profileImageUrl: String?
init(dictionary: [String: AnyObject]){
id = dictionary["id"] as? String
name = dictionary["name"] as? String
statusTxt = dictionary["statusTxt"] as? String
profileImageUrl = dictionary["profileImageUrl"] as? String
}
override var description : String {
let nameString = name != nil ? name! : "nil"
let profileImageUrlString = profileImageUrl != nil ? profileImageUrl! : "nil"
return "Friendship:\nname = \(nameString),\nprofileImageUrl = \(profileImageUrlString)"
}
}
我怎麼能知道/查看/打印,如果friends.append功能是否正常工作和填充確定所有的友誼NSObject的屬性?
大Joern,但你可以在編輯的代碼看,遇到的問題Im是點的友誼NSObject的有2屬性,朋友和用戶,它們是用戶NSObject的引用,我不知道如何在函數**朋友中填充這些屬性。append(FriendShip(字典:詞典))**以及如何使用用戶NSObject的值打印完整的FriendShip NSObject – Edu
若要在'Friendship'類中打印User屬性的值,還必須覆蓋User類中的'description'屬性。然後當你調用'print(friendship)' – joern
對不起,但是如果我設置了** let nameString = name!= nil,那麼你會自動獲得這兩個屬性的值。名稱! :在用戶對象的覆蓋函數中的「nil」**和** let friendString = friend.name!= nil? friend.name! :友誼對象的覆蓋函數中的「nil」**,這最後一個聲明通過一個EXC_BAD_INSTRUCTION錯誤 – Edu