0

i have a question about array in Swift.

if i have variables like this :

var name = [“cafe A”, “Cafe B”]
var image = [“cafeaimg.jpg”, “cafebimg.jpg”]
var fav = [false, false]

i’ll write a class like this :

class cafe {
    var name:String = “”
    var image:String = “”
    var fav:Bool = false

    init(name:String, image:String, fav:Bool) {
        self.name = name
        self.image = image
        self.fav = fav
}
}

my question : how i can write a class with a CGFloat variable like this :

var image = [“image1.jpg”, “image2.jpg”]
var calc : [[CGFloat]] = [
    [77, 53, 223, 53, 77, 247, 223, 247],
    [63, 34, 237, 34, 63, 265, 237, 265]
    ]
hermeneutic
  • 45
  • 2
  • 9

1 Answers1

1

If you're confused about how to set the CGFloat values in an init function, you could do something like this:

class myNewClass
{
    var calc : [[CGFloat]] = [[77, 53, 223, 53, 77, 247, 223, 247],[63, 34, 237, 34, 63, 265, 237, 265]]

    init(index:Int, value:CGFloat...)
    {
        calc[index] = value
    }
}

If you then instantiate myNewClass with:

let newClass:myNewClass = myNewClass(index: 0, value: 1, 2, 3)

Then later say:

println(newClass.calc.count)
println(newClass.calc[0])

This would print:

2
[1.0, 2.0, 3.0]

If, during initialization, you want to add an element, you would of course use append.

Edit:

If all you want to do is associate your CGFloat arrays with particular images, you could use a dictionary and you don't really need to create a separate class to do it.

If, for example, I put the following line in my appDelegate file, but outside of (above) any class

var calcDict:[String:[CGFloat]] = ["image1.jpg": [77, 53, 223, 53, 77, 247, 223, 247], "image2.jpg":[63, 34, 237, 34, 63, 265, 237, 265]]

Because it's declared outside of any class, it's now visible from anywhere in your code.

If then, somewhere else in my code I write:

var myCGFloats = calcDict["image1.jpg"]!
println(myCGFloats)

calcDict["image3.jpg"] = [1, 2, 3]
myCGFloats = calcDict["image3.jpg"]!
println(myCGFloats)

This prints:

[77.0, 53.0, 223.0, 53.0, 77.0, 247.0, 223.0, 247.0]
[1.0, 2.0, 3.0]

The return from a dictionary lookup is an optional type, so I put an ! after each of the myCGFloats = statements to unwrap the value returned from the dictionary.

If you want to declare calcDict in your view controller class you could also do that. Inside your view controller class (let's call it MyViewController), but outside of any function (like init()) all you need to have is:

var calcDict:[String:[CGFloat]] = ["image1.jpg": [77, 53, 223, 53, 77, 247, 223, 247], "image2.jpg":[63, 34, 237, 34, 63, 265, 237, 265]]

This creates an instance variable. You could then access calcDict directly from inside your view controller, or if you have instantiated your view controller as:

myViewController = MyViewController()

then from anywhere in your code where myViewController is visible you could access calcDict as:

var myCGFloats = myViewController.calcDict["image1.jpg"]!
jwlaughton
  • 905
  • 1
  • 6
  • 11
  • Hello, thank for your answer. but i'm sorry i'm newbie in programming.. :) but i have another question for your answer. how i can use the "calc" based on selected image? so it will be like "for image1, use calc 1" "for image2, use calc2". and where do i have to write the calc? in the myNewClass class (in your example) or in the view controller where i instantiate it? – hermeneutic Feb 16 '15 at 05:11
  • I've edited my answer. Please let me know if this is not what you're looking for. – jwlaughton Feb 16 '15 at 07:29