简体   繁体   English

如何使用swift从[Any]中访问密钥

[英]How to access key from [Any] using swift

Am developing a application using swift,in my am integrating cometchat following the link https://docs.cometchat.com/ios-sdk/quick-start/ . 我正在使用swift开发应用程序,我正在通过链接https://docs.cometchat.com/ios-sdk/quick-start/集成cometchat。 So am fetching chat history from the server. 因此,正在从服务器获取聊天记录。

Now I got two(sender messages and receiver messages) nsmutablearray value from the server. 现在,我从服务器获得了两个(发送者消息和接收者消息)nsmutablearray值。

ie, var firstArr = response!["history"]! as! NSMutableArray 即, var firstArr = response!["history"]! as! NSMutableArray var firstArr = response!["history"]! as! NSMutableArray

   var secondArr = response!["history"]! as! NSMutableArray

I merged two nsmuatblearray values and the result i got is: 我合并了两个nsmuatblearray值,得到的结果是:

 [
    {
        from = 1;
        id = 68;
        localmessageid = "46A9A5E5-FEEC-4588-B7D6-18E88BA68393_2";
        message = "ckeck messages";
       "message_type" = 10;
        old = 1;
        self = 1;
        sent = 1521185409000;
    }, 
    {
        from = 2;
        id = 69;
        localmessageid = "46A9A5E5-FEEC-4588-B7D6-18E88BA68393_1";
        message = "sent to thiyakarajan";
       "message_type" = 10;
        old = 1;
        self = 1;
        sent = 1521185410000;
    }
 ]

In the responses key value sent is refered as timestamp. 在响应中,发送的键值称为时间戳。

using that sent key(timestamp) help me to sort array in ascending order. 使用发送的键(时间戳)可以帮助我按升序对数组进行排序。

And also me to load message value in table view. 还有我要在表视图中加载消息值。

Thanks 谢谢

You can access the Array in cellforrowatindexpath method of your TableView by using code like [[yourarray objectAtIndex:indexPath.row]objectForKey:@"message"] . 您可以使用[[yourarray objectAtIndex:indexPath.row]objectForKey:@"message"]类的代码访问TableView的cellforrowatindexpath方法中的Array。 By using this line you can access the value of message of the array at indexPath 0. Also for sorting the Array depending on timestamp please refer the below code: 通过使用此行,您可以在indexPath 0处访问数组的message值。另外,根据时间戳对数组进行排序,请参见以下代码:

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"sent" ascending: YES];
return [myArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

We have used the same in our ReadyUI code. 我们在ReadyUI代码中使用了相同的内容。 Please refer our ReadyUI source code and email us at support@cometchat.com if you need additional assistance. 请参考我们的ReadyUI源代码,如果需要其他帮助,请发送电子邮件至support@cometchat.com。

It's a very bad practice working with network data like that. 像这样处理网络数据是非常糟糕的做法。 Especially if you want to use this data further inside of your app. 特别是如果您想在应用程序内部进一步使用这些数据。

  1. You need to parse the data received either in Dictionary or in your own data structure to work with it safely. 您需要解析在Dictionary或在您自己的数据结构中接收到的数据,以安全地使用它。 There're plenty of ways to do that including JSONSerialization , Codable Protocol or even external libraries like SwiftyJSON 有很多方法可以做到这一点,包括JSONSerializationCodable Protocol甚至SwiftyJSON外部库
  2. After parsing you will have your objects that you can work with without necessity of casting Any type to expected types, which brings a lot of optional checks. 解析之后,您将拥有可以使用的对象,而无需将Any类型强制转换为期望的类型,这带来了很多可选检查。 So let's say you have your structure called Message that contains all the data from JSON, after getting all the objects you'll have your neat [Message] array that you can pass around the application. 因此,假设您拥有名为Message的结构,该结构包含JSON中的所有数据,在获取所有对象之后,您将拥有可以在应用程序中传递的整齐的[Message]数组。
  3. To sort array by some of the properties you simply use function sorted(by:) that is default for any collection. 要按某些属性对数组进行排序,您只需使用对任何集合默认的sorted(by:)函数。 In your case: sortedArray = myArray.sorted(by: {$0.sent < $1.sent}) 在您的情况下: sortedArray = myArray.sorted(by: {$0.sent < $1.sent})
  4. Now you can use this array as a dataSource for your tableView using UITableViewDataSource protocol 现在,您可以使用UITableViewDataSource协议将此数组用作tableView的数据源

It's obvious that you're pretty new to iOS programming, so I'd recommend to look up for some tutorials on working with networking data. 显然,您是iOS编程的新手,所以我建议您查找有关使用网络数据的一些教程。 You can watch this video for example, it pretty much sums up everything mentioned above. 例如,您可以观看此视频 ,它几乎总结了上面提到的所有内容。

Do code like this 做这样的代码

Save Swift Array
var firstArr : Array<Dictionary<String, Any>> = response!["history"]! as! Array<Dictionary<String, Any>>

Access valus
let strMessage : String  = fistArr[index]["message"] as! String

Will works..! 会工作..!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM