2

I have a classe named "whisky builder" which only initiates the new Whisky. Now i would like to add the new added whiskies in my "WhiskyOverViewController". But I face the following problem:

class WhiskyOverViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var whiskyArray = [WhiskyBuilder]()
let stringArray = whiskyArray.map({$0.whiskyName!})
var whiskies = [Character: [String]]()
var objectsArray = [Object]()

In the line of "stringArray" I get the error "Instance member 'whiskyArray' cannot be used on type 'WhiskyOverViewController'. Why am I not able to use the whiskyArray-variable there?

Thanks in advance for your help

Mikey
  • 131
  • 1
  • 2
  • 10

3 Answers3

6

What you need there is a read only computed property:

var stringArray: [String] { 
    return whiskyArray.map{$0.whiskyName!} 
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
2

You need to move this code to a function:

let stringArray = whiskyArray.map({$0.whiskyName!})
MirekE
  • 11,515
  • 5
  • 35
  • 28
0

The two types must be incompatible with each other. Just like you cannot assign a UIImage to a String, your program won't let you assign a [WhiskyBuilder] array type to a WhiskyOverViewController type. You must have declared stringArray globally, because otherwise Swift would infer its type.

Andrew Kirna
  • 1,346
  • 16
  • 18