search

Home  >  Q&A  >  body text

iOS: Null pointer reference when encoding non-null enum type?

The structure is roughly like this. Class A contains an array of class B, and class B contains a variable of enumeration type C.
A is initialized as shown in the figure when it is first initialized10 B elements come out, except that the B array in class A has not been modified. However, when encoding s in B, (enumeration C is Status), the value of s can be printed out
, But an error was reported after encode (s) (I put breakpoints on both print and encode, and the latter crashed as soon as I left it)

func encode(with aCoder: NSCoder) {
        print(status)
        aCoder.encode(status, forKey: "status")
    }

The following is some console information:

Normal
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x608000444fb0'


At the same time, there is Thread 1: signal SIGABRT at the top of AppDelegate. After searching on Google, it seems that it is because the object was released early, but the value can still be printed before encode.


The value can be typed, but the value is not the expected value. In a loop before encoding, the value of each B enumeration type of the A class B array is replaced one by one (not directly replacing the object B)

淡淡烟草味淡淡烟草味2740 days ago892

reply all(1)I'll reply

  • 阿神

    阿神2017-05-31 10:38:30

    The reason has been found. The reason is that direct compilation of enumeration types will cause various problems. Just save and read the value of the enumeration to save the country

    reply
    0
  • Cancelreply