私のPHPスクリプトは、下記の形式でJSON応答を私の高速iOSアプリケーションに送信します。NSArrayからのデータの抽出
class Friend : NSObject {
var name: String?
var id: String?
var profile_pic : String?
}
class Message : NSObject {
var id: String?
var text: String?
var date: NSDate?
var sender: Friend?
}
class Chat : NSObject {
var id : String?
var chat_partner : Friend?
var chat_messages : [Message]?
}
私が今やりたいことはNSArray
をループしている:私は私のSWIFTコードに次のオブジェクトを作成している
let jsonArray = try NSJSONSerialization.JSONObjectWithData(data_fixed!, options:[])
:これは、次のコードを使用してNSArray
として解析されますデータをオブジェクトのインスタンスに抽出するので、以下の例で簡単に言えば、idの1と2、およびすべての適切な変数を持つ2つのチャットオブジェクトを作成します。私はどのように迅速に適切にそのようなループを実行するか分からない。
JSON応答:
Array: (
{
chatId = 1;
message = {
1 = {
chatId = 1;
"message_id" = 24242241;
sender = 1233;
text = "hello i am";
timestamp = "2016-05-24 17:13:08";
};
2 = {
chatId = 1;
"message_id" = 421421;
sender = 1233;
text = great;
timestamp = "2016-05-24 17:15:08";
};
};
"user1_id" = 1233;
"user1_name" = David;
"user1_profile_pic" = "http://graph.facebsddsaadsadsook.com/1233/picture?type=large";
"user2_id" = 8543211123;
"user2_name" = 0;
"user2_profile_pic" = "<null>";
},
{
chatId = 2;
"user1_id" = 23413524635;
"user1_name" = 0;
"user1_profile_pic" = "<null>";
"user2_id" = 1233;
"user2_name" = David;
"user2_profile_pic" = "http://graph.facebsdadsook.com/1233/picture?type=large";
}
)
UPDATE:CODE試行
var count_chats = 0;
for anItem in jsonArray as! [Dictionary<String, AnyObject>] {
let curr_chat = Chat()
if let chatId = anItem["chatId"] as? String {
curr_chat.id = chatId
}
let friend = Friend()
let user1id = anItem["user1_id"] as! String
let user2id = anItem["user2_id"] as! String
if (user1id == userID) {
if let user2id = anItem["user2_id"] as? String {
friend.id = user2id
}
if let user2name = anItem["user2_name"] as? String {
friend.name = user2name
}
if let user2profilepic = anItem["user2_profile_pic"] as? String {
friend.profile_pic = user2profilepic
}
}
else if (user2id == userID){
if let user1id = anItem["user1_id"] as? String {
friend.id = user1id
}
if let user1name = anItem["user1_name"] as? String {
friend.name = user1name
}
if let user1profilepic = anItem["user1_profile_pic"] as? String {
friend.profile_pic = user1profilepic
}
}
print("passed")
curr_chat.chat_partner = friend
var chat_messages : [Message]? = nil
var count_messages = 0;
if let dataArray = anItem["message"] as? NSArray {
for onemessage in dataArray as! [Dictionary<String, AnyObject>] {
let curr_message = Message()
if let messageid = onemessage["message_id"] as? String {
curr_message.id = messageid
}
if let messagedate = onemessage["timestamp"] as? NSDate {
curr_message.date = messagedate
}
if let messagesender = onemessage["sender"] as? String {
curr_message.sender = messagesender
}
if let messagetext = onemessage["text"] as? String {
curr_message.text = messagetext
}
chat_messages![count_messages] = curr_message
count_messages = count_messages + 1
}
}
curr_chat.chat_messages = chat_messages
self.user_chats![count_chats] = curr_chat
count_chats = count_chats + 1
だから、それに刺す。 JSONからのあなたのデータは 'AnyObject'型として取り込まれます。あなたはそれを[NSDictionary]にタイプする必要があります。次に、各ディクショナリエントリからキー/値のペアを取得し、その値を適切な型にキャストし、カスタムオブジェクトの配列を構築する必要があります。ここにあなたのコードを載せていれば、それをデバッグするのに役立ちますが、あなたのコードを書くつもりはありません。 –
^^パーフェクト回答 –
@DuncanC私はコードの試行で私の質問を更新しました。あなたは一見をしてください、私のアプローチが正しいかどうか教えてください。 – Alk