NSArray element does not match Swift Array element type

I have a Core Data (Order) object that has a method to return an array of another Core Data (OrderWaypoint) object.

func getOrderedWaypoints() -> [OrderWaypoint] {
    let nameDescriptor = NSSortDescriptor(key: "stop_number", ascending: true)
    let sorted: [OrderWaypoint] = self.waypoints.sortedArrayUsingDescriptors([nameDescriptor]) as [OrderWaypoint]
    return sorted
}

Everything works as expected in the main goal when using the next loop

for waypoint in order.getOrderedWaypoints() {
   // do something
}

But when I try the same in my test goal

fatal error: NSArray element does not match Swift Array element Type

I tried to distinguish the values, but I can not get it to work. Any ideas why this works for the main goal, but not for the test goal?

EDIT

Having broken it well further, the problem arises when I try to get an element from an array.

var orderedList: [OrderWaypoint] = order.getOrderedWaypoints()

for var i = 0; i < orderedList.count; i++ {
   var waypoint = orderedList[i]
   // do something
}
+4
source share
4 answers

sortedArrayUsingDescriptors NSArray, Swift Typed.

func getOrderedWaypoints() -> [OrderWaypoint] {
    let nameDescriptor = NSSortDescriptor(key: "stop_number", ascending: true)
    let sorted = [OrderWaypoint]()
    for elem in self.waypoints.sortedArrayUsingDescriptors([nameDescriptor]) {
        sorted.append(elem)
    }
    return sorted
}
+1

, , , , , :

var orderedWayPoints: [WayPoint] {
    let request = NSFetchRequest(entityName: "Waypoint")
    request.sortDescriptors = NSSortDescriptor(key: "stop_number", ascending: true)
    request.predicate = NSPredicate(format: "parent = %@", self)

    return try! managedObjectContext.executeFetchRequest(request)
}

, , , . .

0

, : self.waypoints OrderWaypoint. , , . OrderWaypoint ( mogenerator, ? - _OrderWaypoint?) , ? KVO .

, , , . ?

self.waypoints, , , .

0

, , , , OrderWaypoint , Swift : App.OrderWaypoint AppTest.OrderWaypoint.

, , :)

: Swift , , @testable import YourAppModuleName Swift. -, , , . (, Swift, , , .)

, , Objective-C, Swift, Objective-C Swift .

Objective-C, ​​ . , , Objective-C, .

Update 2: A slightly weak but simpler solution is to remove the Swift application files from the target and then set the header search paths in the test target, pointing to the folder DerivedSourcesfrom the target application and include the file App-Swift.hin the Objective-C tags that it needs instead of the file AppTest-Swift.h. which no longer has the Swift classes of the application, since you just removed them from the test target.

0
source

All Articles