简体   繁体   English

如何从任意对象的无序数组中获取随机的任意对象?

[英]How do I get a random anyobject from an unordered array of anyobject?

Here's the code I have: 这是我的代码:

if let photosData = parsedResult as? NSDictionary {
    var arrayOfPhotos = photosData.valueForKey("data")!
    // i want to do something like let photo = arrayOfPhotos.random
}

where photosData is photosData在哪里

"data" =    
    (
        { 
            "id"= 1234;
            "name" = "Sam";
        },
        { 
            "id"= 4567;
            "name" = "John";
        },
        { 
            "id"= 1234;
            "name" = "Tony";
        }
    )

Now I am left with arrayOfPhotos which took the value of "data" 现在我剩下的是arrayOfPhotos,它的取值为“ data”

Questions: 问题:

How do I access a random object in arrayOfPhotos? 如何访问arrayOfPhotos中的随机对象?

When I have a variable that xcode doesn't immediately tell me the type of, how do I get the type? 当我有一个xcode不会立即告诉我变量的类型时,如何获取类型?

How do I log variables, objects while debugging? 如何在调试时记录变量,对象? Do I just use print? 我只使用打印吗?

EDIT2: Heavily edited because the question was poorly written. 编辑2:大量编辑,因为问题写得不好。

SOLVED: 解决了:

I added "as? [AnyObject]" to arrayOfPhotos Declaration 我在arrayOfPhotos声明中添加了“ as?[AnyObject]”

if let photosData = parsedResult as? NSDictionary {
    var arrayOfPhotos = photosData.valueForKey("data")! as? [AnyObject]
    // i want to do something like let photo = arrayOfPhotos.random
}

now i can grab an object through arrayOfPhotos.first or grab at an index 现在我可以通过arrayOfPhotos.first抓取一个对象或抓取一个索引

arrayOfPhotos in this case is going to be an NSArray containing an ordered collection of NSDictionary . 在这种情况下, arrayOfPhotos将是一个包含NSDictionary的有序集合的NSArray An NSArray is inherently ordered collection indexed by an integer as you get things out of them in the same order you put them into it. NSArray本质上是有序的集合,由整数索引,当您按照放入它们的顺序从它们中取出东西时。 As opposed to an NSDictionary which is an unordered collection indexed by (in this case) an NSString. NSDictionary相反,后者是由(在这种情况下)NSString索引的无序集合。 The dictionary is unordered because things don't necessarily come out in the same order you put them in, and in fact, come out in an unpredictable order if you were to iterate over all of the items in the dictionary. 字典是无序的,因为事物不一定按您放入的顺序出现,实际上,如果要遍历字典中的所有项目,则它们的出现顺序会变幻莫测。

To answer the exact question you asked, you would be better put to use: 要回答您提出的确切问题,最好使用:

if let photosData = parsedResult as? NSDictionary {
    if let arrayOfPhotos = photosData.valueForKey as? [[String:AnyObject]] {
        // At this point, arrayOfPhotos is an array of dictionaries, so we use direct int indexing:
        let firstPhoto = arrayOfPhotos[0]
        let nthPhoto = arrayOfPhotos[n]

        // Both firstPhoto and nthPhoto now have the type [String:AnyObject], or in otherwords
        //  they map a String to anything, anything because "id" is an int, and "name" is a String
        let firstId = firstPhoto["id"] as! Int
        let firstName = firstPhoto["name"] as! String
    }
}

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

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