Swift 2: Writing to plist with multidimensional objects - arrays

I am a newbie in Swift and I have been trying something for a long time and I am having an compile error that could not overcome with.
I am trying to write to a plist containing multidimensional array objects.
I need to add an array to the inner array of plist.
The plist is like as follows:
I am trying to populate the most inner array of the plist which is as follows:
I am trying to add ITEM 2 under the ITEM 5.
I am using this code:
notesArray.objectAtIndex(0).objectAtIndex(5).addObject("AA","BB","CC","DD")
Compiler gives me following error :
Cannot call value of non-function type '((AnyObject) -> Void)!'
How can I populate the array inside the parent array directly from the code?

Due to value semantics of Swift arrays you have to reassign all changes to their parent objects
This is the initial array
var array : [AnyObject] = [["OZEN PIZZA", "PIZZA", "15", "20", "tariffoto1", [["Biber","2", "Adet", "11"]]]]
get the root array at index 0 of the array
var rootArray = array[0] as! [AnyObject]
get the array at index 5 of rootArray
var item5Array = rootArray[5] as! [[String]]
append the item
item5Array.append(["AA","BB","CC","DD"])
reassign item5Array to index 5 of rootArray
rootArray[5] = item5Array
reassign rootArray to index 0 of the array
array[0] = rootArray

Related

How to freeze an array in swift?

In js I can freeze an array after adding some elements to an array.
Is there anything to freeze an array in Swift?
What is freezing?
Ans: Suppose we have an array. We add some elements
to that array.
/* This is javascript code */
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
// fruits contains some elements
// Now freeze fruits. After freezing, no one can add, delete, modify this array.
Object.freeze(fruits);
My question is here - "Is there anything in swift where we can freeze an array?"
You can create an immutable copy of the array, but the mutability of objects is only controlled by the variable declaration (let for immutable and var for mutable), so once you create a mutable object, you cannot make it immutable or vice-versa.
var fruits = ["Banana", "Orange", "Apple", "Mango"]
fruits.append("Kiwi")
let finalFruits = fruits // Immutable copy
finalFruits.append("Pear") // Gives compile-time error: Cannot use mutating member on immutable value: 'finalFruits' is a 'let' constant

How to access individual elements in an array if the array is stored as a value in a Dictionary

I'm new to coding and this is my first post!
I have created a dictionary in Swift where each individual value is an array.
Ex
1: [0.0443, 0.220832, 0.526799, 0.72147, 0.646954,0.511456,1.00405]
What I need to do is to access the value and store into a different array for data manipulation.
I am having trouble doing this because swift is viewing the array as a single object.
ex. dict[1]!.count will print 1 not 7 (ie. the 7 values)
Is there a way to do this - meaning to get swift to store the value as an array of Doubles?
Thanks.
It would be nice if you shared some code with us. but to access a dictionary of arrays you can do something like this :
let array1 = ["a", "b" , "c"]
let array2 : [Float] = [1.2,2.8,3.4]
let dictionary : Dictionary<String, Any> = ["array1" : array1, "array2" :array2]
var arrayFromDictionary = dictionary["array1"] as! [String]
var array2FromDictionary = dictionary["array2"] as! [Float]
print(arrayFromDictionary[1])
print(array2FromDictionary[2])
the first print call will print out "b" since it is the second member of the array1.
the second print call will print out 3.4 since it is the third member of array2.
does this answer your question ?

How to store a reference to an array element?

I want to store a reference to an array element and modify the array element using the reference.
Example Code:
var myArray : [String] = ["foo"]
var element = myArray.first!
element.append("bar")
print(myArray.first!)
Expected Output:
> foobar
Actual Output:
> foo
My expectation was that first would return a reference to the array element. Instead, Swift returns a copy of the element, meaning the array element doesn't get modified.
Is there a way to store a reference to an array element using Swift arrays?
Swift's String is a value type, so it returns a copy not a reference, if you want to get a reference you should use NSString not String.

Access items from an Array that is inside a Dictionary in Swift

I have a Dictionary which contains an array of fruits and a Double. What I would like to be able to do is access the fruits inside the array.
How can I access items inside the fruits array?
var fruits = ["Apple", "Oranges"]
var fruitDictionary:[String: Any] = ["fruits":fruits, "car":2.5]
print("Dictionary: \(fruitDictionary["fruits"]!)") // output: Dictionary: ["Apple", "Oranges"]
I tried...
print("Dictionary: \(fruitDictionary["fruits"[0]]!)")
and...
print("Dictionary: \(fruitDictionary["fruits[0]"]!)")
But no luck
Thanks
First you need to access the fruits entry of the dictionary and cast it as an array of strings.
From there you can access the elements of the array.
if let array = fruitDictionary["fruits"] as? [String] {
print(array[0])
}
The reason why your attempts did not work is because the values in your dictionary are of type Any, which might not be able to be accessed through a subscript.

Is it possible to append a new object to a heterogenous array in swift?

I know that true mutability can not be achieved in swift. I have an array interspersed with different types of contents.
let myArray = String[]();
var array = ["First","Second","Third",1,0.4,myArray,"dsaa"]
I learned from the above post I have linked that we will be able to append items to an array. But each time I add a new item to the array I have declared above I get this error:
could not find an overload for '+=' that accepts the supplied
arguments
But when the array is homogeneous, I am able to add an item which is same as the already present items, without hassle. but still the item with a different type can not be added.
If you declare your second array explicitly as AnyObject[], you can do it:
let myArray = String[]()
var array:AnyObject[] = ["First", "Second", "Third", 1, 0.4, myArray, "dsaa"]
let n1 = array.count // 7
array += "next"
let n2 = array.count // 8

Resources