2015-10-20 83 views
1

雨燕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的屬性?

回答

2

如果我理解正確,您需要打印/調試Friendship類的內容。

NSObject實施NSObjectProtocol其具有計算屬性description。該屬性返回一個表示該類內容的字符串。這是您打印對象時看到的文字。所以你的Friendship類已經從NSObject繼承了這個description協議。但在你的情況下,它只打印類名,因爲這是默認的實現。

所以,如果你想擁有你的類的更有意義的描述,你必須覆蓋description屬性:

class Friendship: NSObject { 
    var name: String? 
    var age: Int? 

    override var description : String { 
     let nameString = name != nil ? name! : "nil" 
     let ageString = age != nil ? String(age!) : "nil" 
     return "Friendship:\nname = \(nameString),\nage = \(ageString)" 
    } 
} 

我不知道你的實際Friendship類的樣子,所以我做了我自己的,非常簡單的課程,只包含名稱和年齡。

所以,現在當你有Friendship 2個實例(一個充滿和一個空的),你打印出來,可以看到的是,內容對象:

let friendship1 = Friendship() 
friendship1.name = "John" 
friendship1.age = 34 
print(friendship1) 

let friendship2 = Friendship() 
print(friendship2) 

打印:

Friendship: 
name = John, 
age = 34 

Friendship: 
name = nil, 
age = nil 

UPDATE

在你的情況下,重寫的描述變量應該看起來像這樣:

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 friendDescription = friend != nil ? friend!.description : "nil" 
     let userIdString = userId != nil ? userId! : "nil" 
     return "Friendship:\nfriend = \(friendDescription),\nuserId = \(userIdString)" 
    } 
} 

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 "User:\nname = \(nameString),\nprofileImageUrl = \(profileImageUrlString)" 
    } 
} 

你可以測試一下:

// test empty object 
let friendship = FriendShip(dictionary: [String: AnyObject]()) 
print(friendship.description) 

// test correct object 
let user = User(dictionary: ["name": "John", "profileImageUrl": "http://image.com"]) 
let friendship2 = FriendShip(dictionary: ["friend": user, "userId": "1"]) 
print(friendship2.description) 

打印

Friendship: 
friend = nil, 
userId = nil 

Friendship: 
friend = User: 
name = John, 
profileImageUrl = http://image.com, 
userId = 1 
+0

大Joern,但你可以在編輯的代碼看,遇到的問題Im是點的友誼NSObject的有2屬性,朋友和用戶,它們是用戶NSObject的引用,我不知道如何在函數**朋友中填充這些屬性。append(FriendShip(字典:詞典))**以及如何使用用戶NSObject的值打印完整的FriendShip NSObject – Edu

+0

若要在'Friendship'類中打印User屬性的值,還必須覆蓋User類中的'description'屬性。然後當你調用'print(friendship)' – joern

+0

對不起,但是如果我設置了** let nameString = name!= nil,那麼你會自動獲得這兩個屬性的值。名稱! :在用戶對象的覆蓋函數中的「nil」**和** let friendString = friend.name!= nil? friend.name! :友誼對象的覆蓋函數中的「nil」**,這最後一個聲明通過一個EXC_BAD_INSTRUCTION錯誤 – Edu

相關問題