gpt4 book ai didi

ios - 如何知道附加函数是否正在填充 NSObject 的属性

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:56 24 4
gpt4 key购买 nike

swift 2.0 Alamofire 2.0 Xcode 7 IOS 9

我有下一个函数调用 API 并以 JSON 格式检索 friend 列表,转换字典中的列表并将其附加到 Friendship 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)
}

}

print(dictionary) 的结果是:

["_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";
}]

类友谊和用户

 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 函数是否正常工作以及它填充 OK Friendship NSObject 的所有属性?

最佳答案

如果我对你的理解正确,你需要一种方法来打印/调试你的Friendship类的内容。

NSObject 实现了 NSObjectProtocol,它有一个计算属性 description。该属性返回一个表示类内容的字符串。它是您打印对象时看到的文本。所以你的 Friendship 类已经从 NSObject 继承了这个 description 协议(protocol)。但在您的情况下,它只打印类名,因为这是默认实现。

所以如果你想对你的类有一个更有意义的描述,你必须重写 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 类是什么样的,所以我创建了自己的非常简单的类,它只包含一个名字和一个年龄。

所以现在当您有 2 个 Friendship 实例(一个填充一个空)并打印它们时,您可以看到这些对象的内容:

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

更新

在您的情况下,覆盖的描述变量应如下所示:

    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

关于ios - 如何知道附加函数是否正在填充 NSObject 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33246022/

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