0

I'm trying to create an array from the first 10 objects of another array but I get an error:

Cannot assign value of type 'ArraySlice' to type '[AnyObject]'

var latitudeArray: [AnyObject] = []
var tenLat: [AnyObject] = []

func getSaved() {
    if (PFUser.currentUser()?.objectForKey("Latitude") == nil) {
        latitudeArray = []
    } else {
        latitudeArray = PFUser.currentUser()?.objectForKey("Latitude") as! [AnyObject]
    }    

    tenLat = latitudeArray.prefix(10)
    }
0

2 Answers 2

4

You need to turn your ArraySlice into an Array.

tenLat = Array(latitudeArray.prefix(10))

0

you should cast implicitly to [AnyObject]

var latitudeArray: [AnyObject] = []
var tenLat: [AnyObject] = []

func getSaved() {
    if (PFUser.currentUser()?.objectForKey("Latitude") == nil) {
        latitudeArray = []
    } else {
        latitudeArray = PFUser.currentUser()?.objectForKey("Latitude") as! [AnyObject]
    }

    tenLat = [AnyObject](latitudeArray.prefix(10))
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.