I'm trying to wrap my head around initialising empty arrays in Swift.
For an array of strings it's pretty straight forward :
var myStringArray: String[] = []
myStringArray += "a"
myStringArray += "b"
-> ["a", "b"]
and for integers
var myIntArray: Int[] = []
myIntArray += 1
myIntArray += 2
-> [1, 2]
it also works for other types of object such as NSImage objects :
let path = "/Library/Application Support/Apple/iChat Icons/Flags/"
let image1 = NSImage(byReferencingFile: path + "Brazil.png")
let image2 = NSImage(byReferencingFile: path + "Chile.png")
var myImageArray: NSImage[] = []
myImageArray += image1
myImageArray += image2
-> [<NSImage 0x7fe371c199f0 ...>, <NSImage 0x7fe371f39ea0 ...>]
However I can't work out the syntax to initialise an empty array of Dictionaries.
I know you can have an array of Dictionaries because initialising with an initial value works :
let myDict1 = ["someKey":"someValue"]
let myDict2 = ["anotherKey":"anotherValue"]
var myDictArray = [myDict1]
myDictArray += myDict2
-> [["someKey": "someValue"], ["anotherKey": "anotherValue"]]
This however (which you'd expect the syntax to be) fails :
var myNewDictArray: Dictionary[] = []
with the error Cannot convert the expression's type 'Dictionary[]' to type 'Hashable'
So the question is what is the correct way to initialise a empty array of Dictionary Items and why doesn't this syntax var myNewDictArray: Dictionary[] = [] work?
You need to give types to the dictionaries:
var myNewDictArray: [Dictionary<String, Int>] = []
or
var myNewDictArray = [Dictionary<String, Int>]()
Edit: You can also use the shorter syntax:
var myNewDictArray: [[String:Int]] = []
or
var myNewDictArray = [[String:Int]]()
This will create an empty, immutable dictionary:
let dictionary = [ : ]
And if you want a mutable one:
var dictionary = [ : ]
Both of these dictionaries default to Dictionary<String?, AnyObject?>.
The reason this isn't working:
var myNewDictArray: Dictionary[] = []
is that you need to provide types for the keys and values of a dictionary when you define it. Each of these lines will create you an empty array of dictionaries with string keys and string values:
var dictArray1: Dictionary<String, String>[] = Dictionary<String, String>[]()
var dictArray2: Dictionary<String, String>[] = []
var dictArray3 = Dictionary<String, String>[]()
You can no longer use element concatenation.
class Image {}
Image i1
Image i2
var x = [Image]()
x += i1 // will not work (adding an element is ambiguous)
x += [i1] // this will work (concatenate an array to an array with the same elements)
x += [i1, i2] // also good
var yourArrayname = [String]() // String or anyOther according to need
You can use this if u want to use swift 2.3!
let path = "/Library/Application Support/Apple/iChat Icons/Flags/"
let image1 = UIImage(contentsOfFile: readPath + "Brazil.png")
let image2 = UIImage(contentsOfFile: readPath + "Chile.png")
var myImageArray = [UIImage]()
myImageArray.append(image1)
myImageArray.append(image2)
Related
i have two arrays like these
var arr1 = ["han", "Ji", "Kidda", "Ho", "Tusi"]
var arr2 = ["hello", "Ji"]
i want to create a new dictionary that have first element of first array and first element of second array and so on. when the third element of first array comes it should again get the first element of second array.
for example:-
dict = ["han" : "hello", "Ji" : "Ji", "Kidda" : hello, "Ho" : "Ji", "Tusi" : "hello"]
If the second array has 2 items you can do
var dict = [String: String]()
for (index, item) in arr1.enumerated() {
dict[item] = arr2[index % 2]
}
I believe this is what you're looking for (using arr1 as the keys and arr2 as the values repeating them as necessary):
var arr1 = ["han", "Ji", "Kidda", "Ho", "Tusi"]
var arr2 = ["hello", "Ji"]
let dict = Dictionary(uniqueKeysWithValues: zip(arr1, arr1.indices.map { arr2[$0 % arr2.count] }))
print(dict)
["Kidda": "hello", "Ji": "Ji", "han": "hello", "Ho": "Ji", "Tusi": "hello"]
Note:
Dictionaries have no specified ordering. Only the key/value pairings matter. This matches the example in your question.
Explanation:
zip is used to create a sequence of (key, value) tuples from two sequences that will become the key/value pairs for the new Dictionary. The keys come from arr1. map is used to generate the sequence of values from arr2 repeating them as many times as necessary to match the count of arr1. This sequence of (key, value) tuples is passed to Dictionary(uniqueKeysWithValues:) to turn that sequence into the desired Dictionary.
try:
var dict = ["arr1" : "hello", "arr2" : "Ji"]
then for third you can append by
dict[3] = ["arr3" : String(arr3.first())]
Try this:
var arr1 = ["han", "Ji", "Kidda", "Ho", "Tusi"]
var arr2 = ["hello", "Ji"]
var dict : [String : String] = [:]
var arr2Index = 0
for index in 0..<arr1.count {
let arr1Value = arr1[index]
if arr2Index == arr2.count {
arr2Index = 0
}
let arr2Value = arr2[arr2Index]
dict[arr1Value] = arr2Value
arr2Index += 1
}
Here's a fun way:
let arr1 = ["han", "Ji", "Kidda", "Ho", "Tusi"]
let arr2 = ["hello", "Ji"]
let arr3 = Array(repeating: arr2, count: arr1.count).joined()
let d = zip(arr1,arr3).reduce(into: [String:String]()) { $0[$1.0] = $1.1 }
I already have several arrays for strings. I want to add them to a list of arrays, so I can use a predefined (enum) index. The individual arrays are already in place, I only need to access them via index (fixed with enum or within a loop with index from enum type). So there should be no copy of the strings within the array, only a reference to the array itself.
I already have this in mind:
enum TypeOfArray: Int {
case Src = 0, Dest, SrcCache, DstCache, N
}
var srcFolders : [String] = []
var dstFolders : [String] = []
var srcFoldersCache : [String] = []
var dstFoldersCache : [String] = []
var allFolders: [[String]] = []
Then I want to initilaze the main array by assigning each of the individual arrays. But this is rejected by the compiler: ("Cannot subscript a value of type '[[String]]' with an index of type 'TypeArray'")
allFolders[TypeOfArray.Src] = srcFolders
I don't know if this "typesave" index is even possible.
Can I use a fixed index range 0..N when defining for optimizing memory or speed?
Any ideas?
A dictionary would be a good solution for this:
var dict = [TypeOfArray:[String]]()
dict[TypeOfArray.Src] = srcFolders
Singleton
If you want to share the content of your arrays, an you want the updates to be reflected in your code, you can use a Singleton
final class ImageNameManager {
static let sharedInstance = ImageNameManager()
var srcFolders: [String]
var dstFolders: [String]
var srcFoldersCache: [String]
var dstFoldersCache: [String]
private init() {
// populate: srcFolders, dstFolders, srcFoldersCache, dstFoldersCache
srcFolders = []
dstFolders = []
srcFoldersCache = []
dstFoldersCache = []
}
enum ImageType: Int {
case Src = 0, Dest, SrcCache, DstCache
}
func imageNames(imageType: ImageType) -> [String] {
switch imageType {
case .Src: return srcFolders
case .Dest: return dstFolders
case .SrcCache: return srcFoldersCache
case .DstCache: return dstFoldersCache
}
}
}
Usage
Now you can populate one of your array
ImageNameManager.sharedInstance.dstFolders.append("Hello")
and receives the new data in another section of your code
let dstFolders = ImageNameManager.sharedInstance.imageNames(.Dest)
// ["Hello"]
Update
In order to share the same array across your app you cal also use this code
final class ImageNameManager {
static let sharedInstance = ImageNameManager()
var srcFolders: [String] = []
var dstFolders: [String] = []
var srcFoldersCache: [String] = []
var dstFoldersCache: [String] = []
}
Now alway reference it the array with this code ImageNameManager.sharedInstance.dstFolders, look
ImageNameManager.sharedInstance.dstFolders.append("Hello")
ImageNameManager.sharedInstance.dstFolders.append("World")
ImageNameManager.sharedInstance.dstFolders // ["Hello", "World"]
I rewrite this code from php. And I find it difficult to make it work in swift.
var arrayOfData = [AnyObject]()
for index in 1...5 {
var dict = [String: AnyObject]()
dict["data"] = [1,2,3]
dict["count"] = 0
arrayOfData.append(dict)
}
for d in arrayOfData {
let data = d as AnyObject
// I want to update the "count" value
// data["count"] = 8
print(data);
break;
}
Presumably, you want to update the value inside of arrayOfData when you assign data["count"] = 8. If you switch to using NSMutableArray and NSMutableDictionary, then your code will work as you want. The reason this works is that these types are reference types (instead of value types like Swift arrays and dictionaries), so when you're working with them, you are referencing the values inside of them instead of making a copy.
var arrayOfData = NSMutableArray()
for index in 1...5 {
var dict = NSMutableDictionary()
dict["data"] = [1,2,3]
dict["count"] = 0
arrayOfData.addObject(dict)
}
for d in arrayOfData {
let data = d as! NSMutableDictionary
data["count"] = 8
print(data)
break
}
Assuming your array has to be of form '[AnyObject]' then something like this:
var arrayOfData = [AnyObject]()
for index in 1...5 {
var dict = [String: AnyObject]()
dict["data"] = [1,2,3]
dict["count"] = 0
arrayOfData.append(dict)
}
for d in arrayOfData {
// check d is a dictionary, else continue to the next
guard let data = d as? [String: AnyObject] else { continue }
data["count"] = 8
}
But preferably your array would be typed as an array of dictionaries:
var arrayOfData = [[String: AnyObject]]()
for index in 1...5 {
var dict = [String: AnyObject]()
dict["data"] = [1,2,3]
dict["count"] = 0
arrayOfData.append(dict)
}
for d in arrayOfData {
// swift knows that d is of type [String: AnyObject] already
d["count"] = 8
}
EDIT:
So the issue is that when you modify in the loop, you're creating a new version of the dictionary from the array and need to transfer it back. Try using a map:
arrayOfData = arrayOfData.map{ originalDict in
var newDict = originalDict
newDict["count"] = 8
return newDict
}
The most efficient way would be to find the index of the relevant values entry, and then replace that entry. The index is essentially just a pointer into the hash table, so it's better than looking up by key twice:
To update all the entries, you can loop through the indices one at a time:
for i in dictionary.values.indices {
dictionary.values[i].property = ...
}
To update a particular key, use:
let indexToUpdate = dictionary.values.index(forKey: "to_update")
dictionary.values[i].property = ...
For example, I have an array like var myArray = ['player_static.png', 'player_run0.png', 'player_run1.png', 'player_run2.png', 'player_jump0.png', 'player_jump1.png']
Is there any simple way to get only the "player_runX.png" images?
You can use filter to get all elements that hasPrefix("player_run"):
let myArray = ["player_static.png", "player_run0.png", "player_run1.png", "player_run2.png", "player_jump0.png", "player_jump1.png"]
let playerRuns = myArray.filter{$0.hasPrefix("player_run")}
print(playerRuns) //["player_run0.png", "player_run1.png", "player_run2.png"]
One way to do this would be to iterate over the array and retrieve the elements that match the pattern. A very quick sample would be something like this:
var myArray = ["player_static.png", "player_run0.png", "player_run1.png", "player_run2.png", "player_jump0.png", "player_jump1.png"]
func getSubArray(array:[String],prefix:String) -> [String]
{
var newArray = [String]()
for img in array
{
if img.substringToIndex(img.startIndex.advancedBy(prefix.characters.count)) == prefix
{
newArray.append(img)
}
}
return newArray
}
var test = getSubArray(myArray, prefix: "player_run")
What woudl be a simple way to reduce a string like AAA:111;BBB:222;333;444;CCC:555 to a dictionary in Swift. I have the following code:
var str = "AAA:111;BBB:222;333;444;CCC:555"
var astr = str.componentsSeparatedByString(";").map { (element) -> [String:String] in
var elements = element.componentsSeparatedByString(":")
if elements.count < 2 {
elements.insert("N/A", atIndex: 0)
}
return [elements[0]:elements[1]]
}
The code above produces an Array of Dictionaries:
[["A": "111"], ["BBB": "222"], ["UKW": "333"], ["UKW": "444"], ["CCC": "555"]]
I want it to produce
["A": "111", "BBB": "222", "UKW": "333", "UKW": "444", "CCC": "555"]
but no mater what I try, since i call the map function on an Array it seems impossible to convert the nature of the map function's result.
NOTE: The dictionary in string format is described as either having KEY:VALUE; format or VALUE; format, in which case the mapping function will add the "N/A" as being the key of the unnamed value.
Any help on this matter is greatly appreciated.
Your map produces an array of dictionaries. When you want to combine them into 1, that's a perfect job for reduce:
func + <K,V>(lhs: Dictionary<K,V>, rhs: Dictionary<K,V>) -> Dictionary<K,V> {
var result = Dictionary<K,V>()
for (key, value) in lhs {
result[key] = value
}
for (key, value) in rhs {
result[key] = value
}
return result
}
var str = "AAA:111;BBB:222;333;444;CCC:555"
var astr = str
.componentsSeparatedByString(";")
.reduce([String: String]()) {
aggregate, element in
var elements = element.componentsSeparatedByString(":")
if elements.count < 2 {
elements.insert("N/A", atIndex: 0)
}
return aggregate + [elements[0]:elements[1]]
}
print(astr)
Swift has no default operator to "combine" two Dictionaries so you have to define one. Note that the + here is not commutative: dictA + dictB != dictB + dictA. If a key exist in both dictionaries, the value from the second dictionary will be used.
This is a work for reduce:
let str = "AAA:111;BBB:222;333;444;CCC:555"
let keyValueStrings = str.componentsSeparatedByString(";")
let dictionary = keyValueStrings.reduce([String: String]()) {
aggregate, element in
var newAggregate = aggregate
let elements = element.componentsSeparatedByString(":")
let key = elements[0]
// replace nil with the value you want to use if there is no value
let value = (elements.count > 1) ? elements[1] : nil
newAggregate[key] = value
return newAggregate
}
print(dictionary)
You can also make aggregate mutable directly:
let dictionary = keyValueStrings.reduce([String: String]()) {
(var aggregate: [String: String], element: String) -> [String: String] in
let elements = element.componentsSeparatedByString(":")
let key = elements[0]
// replace nil with the value you want to use if there is no value
let value = (elements.count > 1) ? elements[1] : nil
aggregate[key] = value
return aggregate
}
This is a functional approach, but you can achieve the same using a for iteration.
The reason this is happening is because map can only return arrays. If you are using this method to parse your string, then you need to convert it to a dictionary after.
var newDict = [String:String]()
for x in astr {
for (i, j) in x {
newDict[i] = j
}
}
The current issue with your code is that map function iterates over array containing [["key:value"],["key:value"]..] and you separate it again. But it returns ["key":"value"] which you then add to your array.
Instead you can add elements[0]:elements[1] directly to a locally kept variable which will fix your problem. Something like
finalVariable[elements[0]] = elements[1]