EXC_BAD_ACCESS error while running app - swift

I have this code and whenever i am running app it gives me EXC_BAD_ACCESS.
I printed values for self.state and newValue.rawValue by po in terminal of XCode and they have values but i dont understand why i am getting "EXC_BAD_ACCESS" error and "CoreData: warning: Unable to load class named for entity Class not found, using default NSManagedObject instead". I am getting this error when I do following
d.change = .n //d is just instance of class from where state comes
What can be a reason?
var change: A {
get { return A(rawValue: self.state) ?? .n }
set { self.state = newValue.rawValue }
}
enum A: Int16 {
case a = 1
case b = 2
case c = 3
case n = 90
}
state is a property of class which inherits from NSManagedObject
#NSManaged var state: Int16

Related

Cannot Dynamically Set Enum in Swift with Reflection

I am attempting to dynamically clone a class object in Swift using reflection and the setValue function. This class contains an enum property that is backed with an Int, which is causing this dynamic reflection to crash:
#objc enum Status : Int {
case green
case yellow
case red
}
#objc class State : NSObject {
#objc var status : Status
init(_ status: Status) {
self.status = status
}
}
func testReflectiveClone() {
let state1 = State(.green)
let state2 = State(.yellow)
let state1Mirror = Mirror(reflecting: state1)
for property in state1Mirror.children.enumerated() {
let label = property.element.label
state2.setValue(property.element.value, forKey: label!) //crashes here
}
}
This test function is throwing the following error in XCode:
-[__SwiftValue longLongValue]: unrecognized selector sent to instance 0x600001e5a340 (NSInvalidArgumentException)
Is it even possible to dynamically set enum values? What modification would I need to make to get this to work?
You can add rawValue to avoid the runtime error.
state2.setValue(property.element.value.rawValue, forKey: label!)
Please check the definition of setValue.
open func setValue(_ value: Any?, forKey key: String)
The enum value you passed will be transferred to internal class named "__SwiftValue", the compiler found that "status" was an Int enum type and tried to convert the value to Swift.Int with "longlongvalue", so the runtime error happened.

Strange issue with setter

I have this code and whenever i am running app it gives me EXC_BAD_ACCESS.
I printed values for self.state and newValue.rawValue by po in terminal of XCode and they have values but i dont understand why i am getting "EXC_BAD_ACCESS" error and "CoreData: warning: Unable to load class named for entity Class not found, using default NSManagedObject instead". I am getting this error when I do following
d.change = .n //d is just instance of class from where state comes
What can be a reason?
var change: A {
get { return (A(rawValue: self.state) ?? .none)! }
set { self.state = newValue.rawValue }
}
enum A: Int16 {
case a = 1
case b = 2
case c = 3
case n = 90
}
state is a property of class which inherits from NSManagedObject
#NSManaged var state: Int16
The Unable to load class named for entity Class error means that you have created the entity in the Core Data model editor but that you didn't tell it what class name to use. Core Data doesn't know that the class Class is used with the entity Class, because the names don't have to be the same so Core Data doesn't assume they are. So it uses a plain old NSManagedObject, but warns you about it.
You can't use properties of Class because you don't have one, so your app crashes.
You fix this by going to the Core Data model editor and making sure that the class name for the entity is correct.

RLMException: Object type does not match RLMArray type

I have a simple object:
class MyObject : Object {
dynamic var dummyField: String!;
}
and another object which inherits from MyObject:
class MyOtherObject : MyObject {
dynamic var anotherDummyField: String!;
}
Now I do the following. I have a Realm List of MyObject and I create an instance of MyOtherObject and try to save it inside the list:
class Operator {
internal var myObjects: List<MyObject>!;
internal var myObject: MyObject!;
func operate() {
self.myObject = MyOtherObject();
self.myObject.dummyField = "dummy field";
self.myObject.anotherDummyField = "another dummy field";
self.myObjects = List<MyObject>();
self.myObjects.append(myObject); // crash!
}
}
It crashes with the error:
Terminating app due to uncaught exception 'RLMException', reason: 'Object type 'MyOtherObject' does not match RLMArray type 'MyObject'.'
Since MyOtherObject is a subclass of MyObject I cannot understand why the app is crashing at this point with this error message.
Realm Lists are not covarient. You can only store objects of exactly the declared type in them.
self.myObjects = List<MyObject>();
This is not a proper value
self.myObjects = List<Needs actual object here not class>();

Crash on swift class init when using inline initializers

I'm developing an application and I encountered a crash that I can't explain.
The library is fairly complex so I prepared a minimum example
struct Info {
static let test = 1
}
class SuperCls<A> {}
class Cls<A>: SuperCls<A> {
let v: Info.Type = Info.self
}
let v = Cls<Int>()
This code crashes when I alloc the class, in the very last line, with the following error
file:///play.playground/: error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x4403).
I also tried this in an xcode project and the error is pretty much the same
I found a way to make this code works
struct Info {
static let test = 1
}
class SuperCls<A> {}
class Cls<A>: SuperCls<A> {
var v: Info.Type?
override init() {
v = Info.self
super.init()
}
}
let v = Cls<Int>()
Can someone tell me why this crash occurs?
Thanks!
UPDATE
I also discovered that this works
struct Info {
let test = 1
}
class SuperCls<A> {}
class Cls<A>: SuperCls<A> {
let v = Info()
}
let v = Cls<Int>()
The problem seems to be related to the fact that I'm using Info as a type and accessing static information
Alright, it seems that it is a bug in Xcode beta 5
Thanks for the help

Compiler didn't fire an error when creating an object from a Protocol

Here is the code example from the resource I've reading
protocol Vehicle {
var weight: Int { get }
var name: String { get set }
}
protocol WheeledVehicle: Vehicle {
var numberOfWheels: Int { get }
var wheelSize: Double { get set }
}
class Bike: WheeledVehicle{
var weight: Int = 30
var name: String = "kukulus"
var numberOfWheels: Int = 2
var wheelSize: Double = 16.2}
var bike: Bike = Bike()
bike.numberOfWheels = 16
var wheeledBike: WheeledVehicle = bike // *** There will be an error, in fact not.
For the last line (marked with ***)
Question 1 The book I read claimed that here will be an error fired by the compiler that "Cannot assign to numberOfWheels!", however, it's not. Is the book wrong?
Question 2 var wheeledBike: WheeledVehicle = bike where WheeledVehicle is a protocol, isn't it like an abstract class that protocol cannot instantiate an object? if so, why didn't the compiler give me an error warning?
Update:
If adds the following code, there will be an error notice
wheeledBike.numberOfWheels = 4
The book is not wrong, the above line will introduce an error, not the line marked with ***
Thanks a lot for your time and help
In the following code you are creating a Bike object, which has var numberOfWheels property, var means you can change it and thats why you are able to change the value of numberOfWheels.
var bike: Bike = Bike()
bike.numberOfWheels = 16
The following line works because Bike class conforms to protocol WheeledVehicle, thus bike can be casted as wheeledBike. You are NOT creating a new object, but you are just assigning it to a new variable.
var wheeledBike: WheeledVehicle = bike
If you tried following code, it would fail, because wheeledBike is type of WheeledVehicle which has var numberOfWheels: Int { **get** }, here get means that by protocol, you are only able to retrieve this value, but you are NOT allowed to set/change it.
wheeledBike.numberOfWheels = 16