What I'm looking to try to do is have another var attached to the array to store values in without having to make a 2D array.
var list: NSMutableArray! = NSMutableArray()
list.add("Apple")
list[0].color = red // Something like this
Any help in the right direction will be appreciated
For that make array of custom class or struct.
struct Fruit {
let name: String
let color: String
}
Now make array of this struct and add object of this struct in it.
var fruits = [Fruit]()
fruits.append(Fruit(name: "Apple", color: "red"))
Now you can access object in a way you describe in your question.
print(fruits[0].name) //Apple
print(fruits[0].color) //red
Note: In Swift use swift's type Array and Dictionary instead of NSArray and NSDictionary.
Related
Context - I'm currently learning Swift Struct. So I decided to create my own Phone structure in Playground. (see below)
Problem -
The downloadApp method on the phone struct is throwing the following error.. Cannot use mutating member on immutable value: 'self' is immutable.
Expect outcome - To peacefully append new strings to my apps property, which is an array of strings.
Swift code
struct Phone {
let capacity : Int
let Brand : Sting
var name: String
let model: String
var apps: [String]
var contacts: [String]
var formatCapacity: String {
return "\(capacity)GB"
}
func downloadApp(name: String){
apps.append(name) // ERROR
}
}
You simply need to mark downloadApp as mutating. The issue is caused by the fact that downloadApp is declared in a struct type, which is a value type and hence if you mutate any properties (namely, the apps array here) of the struct, you actually mutate the struct itself. By marking the function as mutating, the compiler allows such modification of the struct through a member function of the type.
mutating func downloadApp(name: String){
apps.append(name)
}
Assume I have an array of structs like so:
struct Record {
let name: String
}
let array = [Record(name: "John"), Record(name: "Jim"), Record(name: "Bob")]
I would like to get the index of each element using UILocalizedIndexedCollation.section(for: collationStringSelector:). The problem is, when I pass:
#selector(getter: record.name)
the following error is returned:
Argument of '#selector' refers to var 'name' that is not exposed to
Objective-C
Is there any way of exposing an instance value in a struct to a #selector? (NB: the struct I am passing is used extensively throughout my app and I don't really want to change it to a class)
Converting the struct variable to an NSString and using one of NSString's methods / variables is a work around that fixed the issue:
let index = UILocalizedIndexedCollation.current().section(for: (record.name as NSString), collationStringSelector: #selector(getter: NSString.uppercased))
I am wondering how to add more elements to my array 'faq'. right now it has just image but i want to string a label and other images. This is Xcode Swift.
var questions : [faq] = faq
var selectQuest = 0;
var doneQuest = Bool()
override func viewDidLoad() {
super.viewDidLoad()
questions.append(faq(fImage: "1.png") here x) or X<-- I guess in here where ar you add more elements?
questions.append(faq(fImage: "2.png"))
questions.append(faq(fImage: "3.png"))
questions.append(faq(fImage: "4.png"))
questions.append(faq(fImage: "5.png"))
questions.append(faq(fImage: "6.png"))
questions.append(faq(fImage: "7.png"))
questions.append(faq(fImage: "8.png"))
dispendserIQGCollectionView.reloadData()
print(questions.count)
See the Swift Documentation: Collection Types.
Alternatively, append an array of one or more compatible items with the addition assignment operator (+=):
shoppingList += ["Baking Powder"]
// shoppingList now contains 4 items
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList now contains 7 items
If I understand your question correctly, you have an array of FAQ. The FAQ class has an member named fImage. However, you want to add more members to class.
I think your class looks something like this -
class FAQ {
var fImage: UIImage
/*Rest of class*/
}
To add more member to the class, you need to add another member named fLabel to the class, as shown below
class FAQ {
var fImage: UIImage
var fLabel: UILabel
/*Rest of class*/
}
the problem may sound weird but is exactly my situation.
I have a class containing a struct with only an array inside: an array of another struct. I would like to store an object of the class into user default. Let see some code
struct Inside {
var something: String
var somethingElse: Int
}
struct Outside {
var array = [Inside]()
}
class TheClass: {
var array = [Outside]()
}
var a = TheClass()
//now I would like to put 'a' into UserDefaults
I tried to use NSCoding, like you can see in this code (the code compiles)
class TheClass: NSObject, NSCoding {
var array: NSMutableArray = NSMutableArray()
override init(){
self.elementi = []
}
required init(coder aDecoder: NSCoder) {
elementi = aDecoder.decodeObjectForKey("elementi") as! NSMutableArray
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(elementi, forKey: "elementi")
}
}
but when I try to add an object of struct Outside into the NSMutableArray, I get an error. I found out that one solution is to convert into an NSValue. Here is the code in objective C, I am not able to translate it in swift
//here 'a' denotes an object of Outside struct that we want to insert into the NSMutableArray stored inside an object of TheClass (we cannot add it directly to the NSMutableArray)
NSValue* value = [NSValue value:&a withObjCType:#encode(Outside)];
//To extract it later:
[value getValue:&value];
I don't know how to solve this problem. Maybe I can change the struct to classes, in this way NSObject is no more need and I can use NSCoding directly (can be used with class object but not with struct object). But if I do this way I will get exactly the error that this guy have: SWIFT: Copying objects from arrays then changing properties and adding them back to original array
What do you suggest?
I'm trying to modify an existing struct which is then used with an array. Is there a solution to the following?
struct pickerData {
var key = ""
var value = ""
}
var pickerArray = [pickerData]()
pickerArray.append(pickerData(key: "1", value: "2")) //OK up to know but
//I need to append a new key:value to this structure
pickerArray.append(pickerData(key: "1", value: "2",value2: "3")) // error
pickerArray.append(pickerData(key: "1", value: "2"),value2: "3") // error
I basically need a mutable struct, is this doable?
The structure of a struct cannot change; as your comment suggests that you want to add a key:value pair, you should be using a datatype that supports such pairs: a Dictionary. (Technically, those aren't key:value pairs in a struct.)