soRazor
soRazor

Reputation: 147

Value of type 'NSArray' has no member 'indices'

I have a separate struct which holds my array of quotes, I imagine that I will be adding up to 100 quotes and wanted it in a separate file. I am attempting to access this array from my HomeScreen view controller however keep getting the error "Value of type 'NSArray' has no member 'indices' ". I am beginning to think I have not taken the correct approach to achieve this.

class Quotes: NSObject {    
    let quotes: NSArray = ["quote1", "quote2","quote50", "quote100"]    
}

class HomeScreen: UIViewController {
    var quotes: NSArray = NSArray()

    func getNextQuote(){
        let defaults = UserDefaults.standard
        defaults.integer(forKey: "savedIndexKey")
        let currentIndex = defaults.integer(forKey: "savedIndexKey")
        var nextIndex = currentIndex+1
        nextIndex = quotes.indices.contains(nextIndex) ? nextIndex : 0 //error here
        defaults.set(nextIndex, forKey: "savedIndexKey")
        let savedInteger = defaults.integer(forKey: "savedIndexKey")
        saved = savedInteger
        quotesLabel.text = quotes[savedInteger]  
    }

}

override func viewDidLoad() {
    var quotesArray: Quotes = Quotes()
    quotes = quotesArray.quotes
}

Upvotes: 0

Views: 772

Answers (1)

vadian
vadian

Reputation: 285092

Don't use NS... collection types in Swift at all.

Replace

let quotes: NSArray = ["quote1", "quote2","quote50", "quote100"]

with

let quotes = ["quote1", "quote2","quote50", "quote100"]

and replace

var quotes: NSArray = NSArray()

with

var quotes = [String]()

Upvotes: 4

Related Questions