在某些情况下,type(of:NSManagedObject())结果错误

时间:2017-01-16 12:25:11

标签: ios swift core-data

我试图在泛型函数中获取NSManagedObject的子类的元类型时遇到了一个奇怪的错误。它不会出现在任何设备上:现在我在iOS 10.1.1(14B100,iPhone 5s)上出现此错误,但所有iOS 10模拟器上都没有错误。 10.2(14C92)iPhone 5c。 Xcode的版本是8.2.1(8C1002)。

整个程序代码位于一个文件中(typeOfCD.xcdatamodeld除外,其中包含两个空实体DataObjectAd,其中Ad的父实体设置为DataObject },并且两个实体“Codegen”=“手动/无”),部署目标= 10.0:

ViewController.swift

import UIKit
import CoreData

public class DataObject: NSManagedObject {}
public class Ad: DataObject {}

class ViewController: UIViewController {
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    override func viewDidLoad() {
        super.viewDidLoad()

        let object: DataObject = Ad(context: context)

        print(object)
        print(type(of: object as! Ad))
        print(type(of: object)) //Ok

        generic_getObject(object)
    }
    func generic_getObject<T: DataObject>(_ object: T) {
        print(object)
        print(type(of: object as! Ad)) //Ok; why this line not crashed, but next - crashed? It's blowing my mind
        print(type(of: object)) //Thread 1: EXC_BAD_ACCESS (code=1, address=0x1a1740dbd01)
    }
}

控制台输出:

<typeOfCD.Ad: 0x1740b1be0> (entity: Ad; id: 0x1740289e0 <x-coredata:///Ad/tFB64279C-AA32-4C31-B933-E468990BCB182> ; data: {
})
Ad
Ad
<typeOfCD.Ad: 0x1740b1be0> (entity: Ad; id: 0x1740289e0 <x-coredata:///Ad/tFB64279C-AA32-4C31-B933-E468990BCB182> ; data: {
})
Ad
(lldb) bt
* thread #1: tid = 0x2a41c, 0x000000010030195c libswiftCore.dylib`_buildNameForMetadata(swift::TargetMetadata<swift::InProcess> const*, (anonymous namespace)::TypeSyntaxLevel, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 84, stop reason = EXC_BAD_ACCESS (code=1, address=0x1a1740dbd01)
  * frame #0: 0x000000010030195c libswiftCore.dylib`_buildNameForMetadata(swift::TargetMetadata<swift::InProcess> const*, (anonymous namespace)::TypeSyntaxLevel, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 84
    frame #1: 0x00000001003021ac libswiftCore.dylib`swift_getTypeName + 424
    frame #2: 0x00000001002b59d4 libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[1] = Owned To Guaranteed> of generic specialization <preserving fragile attribute, Any, Swift._TeeStream<Swift.String, Swift._Stdout> with Swift._TeeStream<Swift.String, Swift._Stdout> : Swift.TextOutputStream in Swift> of Swift.(_adHocPrint_unlocked <A, B where B: Swift.TextOutputStream> (A, Swift.Mirror, inout B, isDebugPrint : Swift.Bool) -> ()).(printTypeName #1) <A, B where B: Swift.TextOutputStream> (Any.Type) -> () + 44
    frame #3: 0x00000001002b5cc4 libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[1] = Owned To Guaranteed and Exploded> of generic specialization <preserving fragile attribute, Any, Swift._TeeStream<Swift.String, Swift._Stdout> with Swift._TeeStream<Swift.String, Swift._Stdout> : Swift.TextOutputStream in Swift> of Swift._adHocPrint_unlocked <A, B where B: Swift.TextOutputStream> (A, Swift.Mirror, inout B, isDebugPrint : Swift.Bool) -> () + 248
    frame #4: 0x00000001001f5ae4 libswiftCore.dylib`generic specialization <preserving fragile attribute, Any, Swift._TeeStream<Swift.String, Swift._Stdout> with Swift._TeeStream<Swift.String, Swift._Stdout> : Swift.TextOutputStream in Swift> of Swift._print_unlocked <A, B where B: Swift.TextOutputStream> (A, inout B) -> () + 1376
    frame #5: 0x00000001002b70ac libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed and Exploded, Arg[2] = Owned To Guaranteed and Exploded> of generic specialization <preserving fragile attribute, Swift._TeeStream<Swift.String, Swift._Stdout> with Swift._TeeStream<Swift.String, Swift._Stdout> : Swift.TextOutputStream in Swift> of Swift._print <A where A: Swift.TextOutputStream> (Swift.Array<Any>, separator : Swift.String, terminator : Swift.String, to : inout A) -> () with unmangled suffix "_merged" + 320
    frame #6: 0x00000001002b6eec libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed and Exploded, Arg[2] = Owned To Guaranteed and Exploded> of generic specialization <preserving fragile attribute, Swift._TeeStream<Swift.String, Swift._Stdout> with Swift._TeeStream<Swift.String, Swift._Stdout> : Swift.TextOutputStream in Swift> of Swift._print <A where A: Swift.TextOutputStream> (Swift.Array<Any>, separator : Swift.String, terminator : Swift.String, to : inout A) -> () + 24
    frame #7: 0x00000001002b7480 libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed and Exploded, Arg[2] = Owned To Guaranteed and Exploded> of Swift.print (Swift.Array<Any>, separator : Swift.String, terminator : Swift.String) -> () with unmangled suffix "_merged" + 208
    frame #8: 0x00000001002b6f3c libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed and Exploded, Arg[2] = Owned To Guaranteed and Exploded> of Swift.print (Swift.Array<Any>, separator : Swift.String, terminator : Swift.String) -> () + 32
    frame #9: 0x00000001001f9208 libswiftCore.dylib`Swift.print (Swift.Array<Any>, separator : Swift.String, terminator : Swift.String) -> () + 32
    frame #10: 0x0000000100023214 typeOfCD`ViewController.generic_getObject<A where ...> (object=<unavailable>, self=<unavailable>) -> () + 496 at ViewController.swift:30
    frame #11: 0x0000000100022e64 typeOfCD`ViewController.viewDidLoad(self=<unavailable>) -> () + 692 at ViewController.swift:25
    frame #12: 0x0000000100023010 typeOfCD`@objc ViewController.viewDidLoad() -> () + 40 at ViewController.swift:0
    frame #13: 0x00000001898550b0 UIKit`-[UIViewController loadViewIfRequired] + 1056
    frame #14: 0x0000000189854c78 UIKit`-[UIViewController view] + 28
    frame #15: 0x000000018985b424 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 76
    frame #16: 0x00000001898588c4 UIKit`-[UIWindow _setHidden:forced:] + 272
    frame #17: 0x00000001898cb0e8 UIKit`-[UIWindow makeKeyAndVisible] + 48
    frame #18: 0x0000000189ad7a78 UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4068
    frame #19: 0x0000000189add5c8 UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1656
    frame #20: 0x0000000189af1e60 UIKit`__84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke.3137 + 48
    frame #21: 0x0000000189ada5ac UIKit`-[UIApplication workspaceDidEndTransaction:] + 168
    frame #22: 0x00000001855a18bc FrontBoardServices`__FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36
    frame #23: 0x00000001855a1728 FrontBoardServices`-[FBSSerialQueue _performNext] + 176
    frame #24: 0x00000001855a1ad0 FrontBoardServices`-[FBSSerialQueue _performNextFromRunLoopSource] + 56
    frame #25: 0x00000001839a8278 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
    frame #26: 0x00000001839a7bc0 CoreFoundation`__CFRunLoopDoSources0 + 524
    frame #27: 0x00000001839a57c0 CoreFoundation`__CFRunLoopRun + 804
    frame #28: 0x00000001838d4048 CoreFoundation`CFRunLoopRunSpecific + 444
    frame #29: 0x00000001898c02b0 UIKit`-[UIApplication _run] + 608
    frame #30: 0x00000001898bb034 UIKit`UIApplicationMain + 208
    frame #31: 0x000000010002551c typeOfCD`main + 140 at AppDelegate.swift:13
    frame #32: 0x00000001828b85b8 libdyld.dylib`start + 4

更新

iPhone 5s设备已更新为10.2(14C92),错误仍然存​​在。

0 个答案:

没有答案