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]
    ]
2
  • what doesn't work about that array of CGFloat arrays? Commented Feb 16, 2015 at 3:24
  • it's work actually, i just want to try another way.. :) Commented Feb 16, 2015 at 5:16

1 Answer 1

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"]!
Sign up to request clarification or add additional context in comments.

2 Comments

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?
I've edited my answer. Please let me know if this is not what you're looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.