swift-array

How to convert NSSet to [String] array?

限于喜欢 提交于 2021-02-17 22:54:13
问题 I have an NSSet of Strings, and I want to convert it into [String]. How do I do that? 回答1: I would use map : let nss = NSSet(array: ["a", "b", "a", "c"]) let arr = nss.map({ String($0) }) // Swift 2 let arr = map(nss, { "\($0)" }) // Swift 1 回答2: If you have a Set<String> , you can use the Array constructor: let set: Set<String> = // ... let strings = Array(set) Or if you have NSSet, there are a few different options: let set: NSSet = // ... let strings1 = set.allObjects as? [String] // or as

How to convert NSSet to [String] array?

青春壹個敷衍的年華 提交于 2021-02-17 22:54:03
问题 I have an NSSet of Strings, and I want to convert it into [String]. How do I do that? 回答1: I would use map : let nss = NSSet(array: ["a", "b", "a", "c"]) let arr = nss.map({ String($0) }) // Swift 2 let arr = map(nss, { "\($0)" }) // Swift 1 回答2: If you have a Set<String> , you can use the Array constructor: let set: Set<String> = // ... let strings = Array(set) Or if you have NSSet, there are a few different options: let set: NSSet = // ... let strings1 = set.allObjects as? [String] // or as

Swift: dictionaries inside array

你离开我真会死。 提交于 2020-01-11 07:17:49
问题 Data: [ { firstName: "Foo", lastName: "Bar" }, { firstName: "John", lastName: "Doe" } ] How can I have this kind of structure using swift array and dictionary? This data shows dictionaries inside an array, right? So I suggest: var persons:Array = [Dictionary<String, String>()] but this gives me the error: Cannot convert the expressions type () to type Array<T> Any ideas? 回答1: The correct way is: var persons = [Dictionary<String, String>]() which is equivalent to: var persons = [[String :

Swift: dictionaries inside array

寵の児 提交于 2020-01-11 07:17:29
问题 Data: [ { firstName: "Foo", lastName: "Bar" }, { firstName: "John", lastName: "Doe" } ] How can I have this kind of structure using swift array and dictionary? This data shows dictionaries inside an array, right? So I suggest: var persons:Array = [Dictionary<String, String>()] but this gives me the error: Cannot convert the expressions type () to type Array<T> Any ideas? 回答1: The correct way is: var persons = [Dictionary<String, String>]() which is equivalent to: var persons = [[String :

Swift - Search using UISearchController and NSPredicate with an array of structs

核能气质少年 提交于 2019-12-25 06:47:05
问题 Currently I'm trying to set up a search bar for my tableView. I want to filter the user's input with NSPredicate to display filtered data from an array of struct objects. But when I try to call .filteredArrayUsingPredicate() on such an array, I get the following error [Course] is not convertible to 'NSArray' . Below is my code and a link to the repo. I'm also open to any better methods of approach. import UIKit import SwiftyJSON class CourseTableViewController: UITableViewController,

Efficiently copying Swift Array to memory buffer for iOS Metal

徘徊边缘 提交于 2019-12-20 15:45:12
问题 I am writing an iOS application using Apple's new Metal framework. I have an array of Matrix4 objects (see Ray Wenderlich's tutorial) that I need to pass in to a shader via the MTLDevice.newBufferWithLength() method. The Matrix4 object is leveraging Apple's GLKit (it contains a GLKMatrix4 object). I'm leveraging instancing with the GPU calls. I will later change this to a struct which includes more data per instance (beyond just the Matrix4 object. How can I efficiently copy the array of

Efficiently copying Swift Array to memory buffer for iOS Metal

谁说我不能喝 提交于 2019-12-20 15:43:08
问题 I am writing an iOS application using Apple's new Metal framework. I have an array of Matrix4 objects (see Ray Wenderlich's tutorial) that I need to pass in to a shader via the MTLDevice.newBufferWithLength() method. The Matrix4 object is leveraging Apple's GLKit (it contains a GLKMatrix4 object). I'm leveraging instancing with the GPU calls. I will later change this to a struct which includes more data per instance (beyond just the Matrix4 object. How can I efficiently copy the array of