Set<NSObject>' does not have a member named 'allObjects' - swift

With the original swift I could turn an NSSet (e.g. of Strings) into a typed array with the following syntax:
var stringArray = exampleSet.allObjects as [String]
With the new update I am getting the above error. What is the best way now to convert the Set into an array?

It looks as if your exampleSet is not an NSSet but a native
Swift Set which was introduced with Swift 1.2 (compare https://stackoverflow.com/a/28426765/1187415).
In that case you can convert it to an array simply with
let array = Array(exampleSet)

Looks like 'set' is a keyword. Try using a different variable name

Related

How do put multiple values into a single key in Swift?

Is there a way to put multiple values in one key in Swift's dictionary, such as 'multimap' in Java?
var multiDic = [String:UserData]()
This is the dictionary I declared. it has this structure.
struct UserData{
var name: String
var id: String
var nickname: String
}
And when I put the value in one key in the dictionary, the value is updated without being stacked.
for key in 1...10 {
multiDic.updateValue(UserData(name:"name+\(key)", id:"id+\(key)", nickname:"nick+\(key)"), forKey: "A")
}
print(multiDic.count)
reults
1
How can I accumulate multiple values on a single key? I looked up a library called 'Buckets'
(https://github.com/mauriciosantos/Buckets-Swift#buckets), but there has been no update since swift 3.0.
Swift dictionary doesn't support this. One work around is to declare the dictionary to hold an array of values, like this:
var multiDic = [String:[UserData]]()
You would have to modify your code for adding values to work with the array instead of updating the value in place.
I haven't tried the "Buckets" project you mention, but for what it's worth, it probably still works either as-is or with minor changes.

Convert Realm list of Strings to Array of Strings in Swift

I'm just starting up with RealmSwift, and I'm trying to store an array of Strings in Realm. It doesn't work, so now I'm using List<String>() as an alternative. However, how do I convert these Realm Lists back to [String] again? And if I can't do that, are there any alternatives?
Thanks
However, how do I convert these Realm Lists back to [String] again
You can simply cast List to Array, because List has Sequence Support:
let list = List<String>()
let array = Array(list)
Bear in mind that by converting to an array you'll lose the 'dynamic' quality of a Realm collection (i.e. you'll receive a static array, whereas keeping the original List will provide automatic updating should the source change). But you can create an array by using an extension, e.g.:-
extension RealmCollection
{
func toArray<T>() ->[T]
{
return self.compactMap{$0 as? T}
}
}
Then use:-
let stringList = object.strings.toArray()
Where object is the realm object, and strings is your field.
Here are the details. how to assign an array in the realm list model.
jim.dogs.append(objectsIn: someDogs)

Convert an array of dictionaries into a set Swift 4

I have an array of dictionaries ([[Double:Double]]) which I want to convert into a Set of dictionaries. My goal is to use the .symmetricDifference to find the differences between two arrays (both are of type [[Double:Double]]). How can I do this?
I found this on hackingwithswift.com and tried to use it but I am getting this error:
Type '[[Double : Double]]' does not conform to protocol 'Hashable'
I have also tried this code...
let array1:[[Double:Double]] = [[4.5:3.678], [6.7:9.2867], [7.3: 8.7564]]
let array2:[[Double:Double]] = [[4.5:3.678], [6.7:9.2867]]
let array3 = Set<[[Double:Double]]>(array1).symmetricDifference(Set(array2)) //On this line I get the error above.
You don't want a Set of [[Double:Double]]. You want a Set of [Double:Double], because those are the objects in the array and you want them to be the objects in the Set.
Thus the right thing will happen if you simply say
let array1:[[Double:Double]] = [[4.5:3.678], [6.7:9.2867], [7.3: 8.7564]]
let set1 = Set(array1)
and so on.
This might require you to update to a newer version of Swift. It works in Swift 4.2.

Type "Any" has no subscript members despite casting as AnyObject on Swift 3?

Recently converted code from earlier version of swift to swift 3. Got a lot of errors signifying that type "any" has no subscript members and I thought this could be fixed by casting as AnyObject, but the error persists (and therefore the code I post here does not have this cast in it). Here is the relevant code:
func textfieldTextWasChanged(_ newText: String, parentCell: CustomCell) {
let parentCellIndexPath = tblExpandable.indexPath(for: parentCell)
var address = ""
address = "\(newText)"
// TODO: add a pin to the map from input address
cellDescriptors[0][11].setValue(address, forKey: "primaryTitle")
location = cellDescriptors[0][11]["primaryTitle"]! as! String
tblExpandable.reloadData()
}
Note that cellDescriptors is defined earlier in the code as an NSMutableArray. The error shows up right after cellDescriptors[0] in both lines that it is in. Not sure how to fix this.
It's because you're using more than one subscript operator, because presumably this is something like an array of arrays. But NSMutableArray's subscript operator returns Any. As a result, cellDescriptors[0] is Any. You try to use [11] on the result, but Any doesn't accept subscripts because it's Any, not a collection type.
Casting to AnyObject doesn't help because AnyObject is also not a collection type.
What you should do is cast cellDescriptors[0] to something that accepts subscripts. The right choice depends on what kind of data you're storing in cellDescriptors, but it's presumably some kind of collection, probably an array type.
Another approach would be to change cellDescriptors to be a Swift type instead of NSMutableArray. You could specifically declare the types for each part of your data structure, and then type casting wouldn't be needed.

Swift Dictionary array

I am using Swift 1.2 and trying to get the correct syntax for this declaration that comes from a previous version of Swift:
var dataSource: Dictionary<String, String> [][] = [[],[]]
The error I get is :
Array types are now written with the brackets around the element type.
I just don't know how to correct it.
The syntax has changed since then. As your error reads:
Array types are now written with the brackets around the element type.
So instead of Dictionary<String, String>[][] you wrap the brackets around the type for example [[Dictionary<String, String>]] or using shorthand syntax: [[[String: String]]]:
var dataSource: [[[String: String]]] = [[],[]]