Orkhan Alizade
Orkhan Alizade

Reputation: 7549

Swift does not see the variable

I want to pass variable into

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 6 // <-- HERE
}

from the

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { ... }

for it I do the next:

class GalleryController: UIViewController {
    var galleryCount = 0 as Int
}

and later in the

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var gallery = myJSON["result"][choosenRow]["gallery"]
    galleryCount = gallery.count
}

I override my galleryCount variable and when I want to use it in

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return galleryCount // <-- HERE
}

I get the error: error: <EXPR>:1:1: error: use of unresolved identifier 'galleryCount' galleryCount

Why? I do not understand this error. Can anyone help me with it?

Upvotes: 3

Views: 128

Answers (1)

Ashish Kakkad
Ashish Kakkad

Reputation: 23872

Try to define variable without value

class GalleryController: UIViewController {
    var galleryCount:Int = 0
}

And initialize its value in viewDidLoad

because the first method which is called in collection delegate is numberOfItemsInSection not cellForItemAtIndexPath

EDIT

First method which is called is numberOfItemsInSection so the galleryCount will stay 0 and your cellForItemAtIndexPath never called.

If you want to use galleryCount then do it in viewDidLoad.

Upvotes: 4

Related Questions