Can't access class object variable - ios

I'm using Swift 2 to make a function that creates a bunch of objects from a class I wrote.
Now, the problem is that when I'm adding a new object I have no problem, I checked multiple times both from the function and from the init() function that it stores the data. (Printing self.myVariable from the init() function works).
However, when later I go to retrive my variable for example like
if let dog: Animal? = Animal.init(id : "12345"){
print(dog?.legs)}
That prints nil, even though I checked in the Animal class and it successfully initialized the Animal object "dog".
If anybody has any idea why this is happening that would be really useful and appreciated.
Here's the link to the actual code, the Animal class was just an example but this is probably more useful : Class & Function

This is in response to your class code here.
Inside of your init function you are never setting the class variable, id. You could set it as follows:
query.getObjectInBackgroundWithId(id as String) { (object: PFObject?, error: NSError?) -> Void in
if error != nil {
print(error)
}
else {
if let atm = object {
print("ATM found")
self.bank = atm["bank"] as? NSString
self.type = atm["type"] as? NSNumber
self.location = atm["location"] as? PFGeoPoint
self.description = atm["description"] as? NSString
// Set id to whatever you'd like here
self.id = atm.objectId!
}
}
}

I'm not familiar with PFQuery but since it's using a completion handler, it is responding asynchronously to the call to getObjectInBackground. That means that when you get your object from init(id:...) it does not yet have a value in the bank field. So result?.bank prints nil because bank is nil, not because result is nil.
Once the completion handler is executed, bank will have its value (if no error occurred) but it would be unlikely that this would happen in the short time between init() and your let result : ATM? = ... condition.

Related

(SIGABRT Attempt to use unknown class projectname)EXC_BAD_ACCESS error on a strongly referenced variable in Xcode 11, Swift 5, iOS 13

TL;DR
I have a class with no public initializers or instances that passes an instance of itself to a closure in another class. It does this through a mirror of the other class. When I go to access that instance from within the closure, I'm getting a EXC_BAD_ACCESS error, but other parameters passed to the closure are clearly accessible and do not result in a bad access error. I have no idea why. See code below for replication in a new project or a playground.
Detailed Explanation
I've been trying to figure out a way to implement class-specific access control, where multiple specific classes have sole access to another class containing variables and functions to be shared between them. All other classes would not have such access. Kind of like a static class, or a Singleton pattern, but with specific, class-named access control.
I thought I had something that would actually work in pure swift, (which is nice for me since I don't know Objective-C, and only started on swift about 16 months ago.) It's done in an almost anti-swift manner, so just bear with me - my goal is to start with something functional and move it towards elegance and beauty from there.
Even though I'm reasonably confident it should all work, I'm encountering a EXC_BAD_ACCESS error in a very unexpected place.
The "class-specific private" class that you are not allowed to access an instance of unless you are on its "okay" list, we can call the Restricted class.
The class(es) that is(are) allowed access to the Restricted class we can call the Accessor class(es).
The programmer must tell the Restricted class to call a function from the Accessor, and "drop in" an instance of the Restricted class by passing it as a parameter to that function. You do this by passing in the name of the function to be called, an instance of the Accessor class on which to call said function, and any parameters that the function would need in addition to the Restricted class instance.
I could make an enormous switch in the Restricted class, each case of which properly calls each function indicated on each of the Accessor classes...but to get around that excessive overhead/setup, I have the name of the function to be called on the Accessor classes passed in as a string, and accessed through a mirror. Since mirrors only reflect properties and not functions, the function must be a property with an assigned closure, instead of a traditional function.
We can call these closures DropClosures, since their purpose is to have the shared, Restricted class dropped into them. In fact we could call this whole pattern the "DropClosure Pattern". (Or maybe anti-pattern, I know it's kind of gruesome as-is.)
The properties of the "shared" instance of the Restricted class are stored internally as a private static dict (as json, basically). To generate an actual instance of itself, the Restricted class uses a private initializer that accepts that dict as a parameter. After a DropClosure runs with said initialized instance, the Restricted class uses a Mirror of that instance to store any changes back in the "shared" dict, and the instance will go out of scope unless a reference is made to it. So after each DropClosure completes its run, the instance passed to it is more or less useless as a representation of the "shared" aspect of the class, intentionally so.
I only do this because there is no way to require that all references to a certain weak reference also be weak. I don't want a class with access to the weak reference to assign a strong reference to the same instance and keep it in memory, that would defeat the access control goal by allowing the instance to be shared outside of its access scope. Since I can't force the instance to expire once the closure has completed, the next best thing is to remove the motivation for doing so by making the object no longer connected to the shared source after the closure completes.
This all theoretically works, and will compile, and will not throw any swift exceptions when run.
The Accessor (or any class that has an instance of an Accessor) calls RestrictedClass.run(), the run code validates the Accessor instance, finds the DropClosure in that instance, and passes in an instance of the Restricted class to that closure.
However, whenever I try to access that instance from within the DropClosure, it gives me the aforementioned bad access error, seemingly on a C or Objective-C level.
As far as I can tell, the instance should be accessible at this point, and none of the variables being used should be dropping out of scope yet.
At this point I'm totally spitballing - is it possible that there is something in the background that prevents a class with no public initializers from being passed through a mirror? Does it have to do with passing it into a closure called from that mirror? Is there some kind of hidden weak reference that's making the instance get ARC'd?
Please note that I've tried discarding the "weak" wrapper object and only passing in the Restricted instance to the closure, and I get the same bad access error. The error is independent of the instance being weakly referenced.
Code:
import Foundation
typealias DropClosureVoid<T: AnyObject & AccessRestricted> = (_ weaklyConnectedInterface: WeaklyConnectedInterface<T>, _ usingParameters: Any?)->Void
typealias DropClosureAny<T: AnyObject & AccessRestricted> = (_ weaklyConnectedInterface: WeaklyConnectedInterface<T>, _ usingParameters: Any?)->Any?
enum AccessError : Error {
case InvalidFunction
case InvalidAccessClass
}
protocol AccessRestricted {
static func run<T:AnyObject>(_ closureName:String, in classObject: T, with parameters:Any?) throws
static func runAndReturn<T:AnyObject>(_ closureName:String, in classObject: T, with parameters:Any?) throws -> Any?
}
///This class contains an instance that should be expected to only temporarily represent the original, even if a strong reference is made that keeps the value in scope.
class WeaklyConnectedInterface<T:AnyObject> {
weak var value:T?
init(_ value: T) {
self.value = value
}
}
class Accessor {
let restrictedClassPassable:DropClosureVoid<RestrictedAccessClass> = { weaklyConnectedInterface, parameters in
print(weaklyConnectedInterface) // **EXC_BAD_ACCESS error here**
//note that the error above happens even if I pass in the instance directly, without the WeaklyConnectedInterface wrapper.
//It's clearly an issue that occurs when trying to access the instance, whether the instance is wrapped in a the class that makes a weak reference to it or not, which means that it is inaccessible even when strongly referenced.
if let parameterDict = parameters as? [String:String] {
print(parameterDict["paramkey"] ?? "nil")
print(weaklyConnectedInterface)
weaklyConnectedInterface.value?.restrictedVariable = "I've changed the restricted variable"
}
}
let anotherRestrictedClassPassable:DropClosureAny<RestrictedAccessClass> = { weaklyConnectedInterface, parameters in
if let parameterDict = parameters as? [String:String] {
print(parameterDict["paramkey"] ?? "nil")
print(weaklyConnectedInterface.value?.restrictedVariable as Any)
return weaklyConnectedInterface.value?.restrictedVariable
}
return nil
}
func runRestrictedClassPassable() throws {
let functionName = "restrictedClassPassable"
print("trying validateClosureName(functionName)")
try validateClosureName(functionName)//this is in case you refactor/change the function name and the "constant" above is no longer valid
print("trying RestrictedAccessClass.run")
try RestrictedAccessClass.run(functionName, in: self, with: ["paramkey":"paramvalue"])
let returningFunctionName = "anotherRestrictedClassPassable"
print("trying validateClosureName(returningFunctionName)")
try validateClosureName(returningFunctionName)
print("trying RestrictedAccessClass.runAndReturn")
let result = (try RestrictedAccessClass.runAndReturn(returningFunctionName, in: self, with: ["paramkey":"ParamValueChanged"]) as! String?) ?? "NIL, something went wrong"
print("result is \(result)")
}
func validateClosureName(_ name:String) throws {
let mirror = Mirror(reflecting: self)
var functionNameIsPresent = false
for child in mirror.children {
if child.label != nil && child.label! == name {
functionNameIsPresent = true
break
}
}
guard functionNameIsPresent else {
print("invalid function")
throw AccessError.InvalidFunction
}
}
}
extension Mirror {
func getChildrenDict() -> [String:Any]
{
var dict = [String:Any]()
for child in children
{
if let name = child.label
{
dict[name] = child.value
}
}
return dict
}
}
class RestrictedAccessClass:AccessRestricted {
private static var shared:[String:Any] = [
"restrictedVariable" : "You can't access me!"
]
private static func validateType<T>(of classObject:T) throws {
switch classObject {
case is Accessor:
return
default:
print("Invalid access class")
throw AccessError.InvalidAccessClass
}
}
var restrictedVariable:String
private init() {
restrictedVariable = "You can't access me!"
}
private init(from json:[String:Any]) {
restrictedVariable = json["restrictedVariable"] as! String
}
static func run<T:AnyObject>(_ closureName:String, in classObject: T, with parameters:Any?) throws {
print("trying validateType(of: classObject) in run")
try validateType(of: classObject)
for child in Mirror(reflecting: classObject).children {
if let childName = child.label {
if childName == closureName {
let dropClosure = child.value as! DropClosureVoid<RestrictedAccessClass>
let selfInstance = RestrictedAccessClass(from:shared)
let interface = WeaklyConnectedInterface(selfInstance)
dropClosure(interface, parameters)
runCleanup(on: selfInstance)//parses any data changed by the end of the drop closure back into the dict for use in future instances. This means you mustn't try using the instance in an async closure. The correct way to do this would be to call run inside of an async closure, rather than putting an anync closure inside of the drop closure.
_ = interface.value
return
}
}
}
}
static func runAndReturn<T:AnyObject>(_ closureName:String, in classObject: T, with parameters:Any?) throws -> Any? {
print("trying validateType(of: classObject) in runAndReturn")
try validateType(of: classObject)
for child in Mirror(reflecting: classObject).children {
if let childName = child.label {
if childName == closureName {
let dropClosure = child.value as! DropClosureAny<RestrictedAccessClass>
let selfInstance = RestrictedAccessClass(from:shared)
let interface = WeaklyConnectedInterface(selfInstance)
let result = dropClosure(interface, parameters)
runCleanup(on: selfInstance)//parses any data changed by the end of the drop closure back into the dict for use in future instances. This means you mustn't try using the instance in an async closure. The correct way to do this would be to call run inside of an async closure, rather than putting an anync closure inside of the drop closure.
_ = interface.value
return result
}
}
}
return nil
}
private static func runCleanup(on instance:RestrictedAccessClass) {
shared = Mirror(reflecting:instance).getChildrenDict()
//once this function goes out of scope(or shortly thereafter), the instance passed will become useless as a shared resource
}
}
Code to encounter error:
I just put this in a new project's AppDelegate.application(didFinishLaunching). You can put all of the code above and below, in order, in a playground and it will break in the same spot, but not as clearly.
let accessor = Accessor()
do {
try accessor.runRestrictedClassPassable()
}
catch {
print(error.localizedDescription)
}
Updates
Whether zombie objects are turned on or off, I'm getting the same error message from Xcode: Thread 1: EXC_BAD_ACCESS (code=1, address=0x1a1ebac696e)
Running an analysis with Command+Shift+B reveals no warnings.
Running with all of the malloc options enabled reveals the following error:
Thread 1: signal SIGABRT, objc[somenumber]: Attempt to use unknown class 0xSomevalue
This just got weird...
Apparently, the "unknown class" is the project. I found this out by selecting the (i) bubble on the inline object inspector for the Restricted instance that was causing the crash. It gives me the following message:
Printing description of weaklyConnectedInterface:
expression produced error: error:
/var/folders/zq/_x931v493_vbyhrfc25z1yd80000gn/T/expr52-223aa0..swift:1:65:
error: use of undeclared type 'TestProject'
Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer<TestProject.RestrictedAccessClass>(bitPattern: 0x103450690)!.pointee)
^~~~~~~~~~~
I thought that maybe this would happen for other classes, so I tested, and it's able to access other project-level classes just fine. Only for this specific instance is the project "namespace" undefined.
Please find below required modifications (not many)... Tested with Xcode 11.2 / iOS 13.2.
1) made interface inout to pass it as-is original, otherwise it somehow copied loosing type information
typealias DropClosureVoid<T: AnyObject & AccessRestricted> =
(_ weaklyConnectedInterface: inout WeaklyConnectedInterface<T>, _ usingParameters: Any?)->Void
typealias DropClosureAny<T: AnyObject & AccessRestricted> =
(_ weaklyConnectedInterface: inout WeaklyConnectedInterface<T>, _ usingParameters: Any?)->Any?
2) fix places of usage (same in two places)
var interface = WeaklyConnectedInterface(selfInstance) // made var
dropClosure(&interface, parameters) // << copy closure args here was a reason of crash
3) ... and that's it - build & run & output
Note: I would recommend to avoid force unwrap and use the following
if let dropClosure = child.value as? DropClosureVoid<RestrictedAccessClass> {
dropClosure(&interface, parameters)
}

realm-cocoa save an empty object rather than object I passed

I met a problem in realm-cocoa 2.8.0(in 2.7.0 it works good) which is when I want to save an object into the realm file, I saw an empty object with default value is saved into the realm rather than the object I created(even the primary key is different.)
Eg.
class XXXRealmObject: RLMObject {
#objc dynamic var id: String = UUID().uuidString.lowercased()
#objc dynamic var name: String = ""
#objc init(name: String) {
self.name = name
super.init()
}
#objc override init() {
super.init()
}
override class func primaryKey() -> String {
return "id"
}
}
let obj = XXXRealmObject(name: "jojo")
let realm = try! RLMRealm(configuration: .default())
try? realm.transaction {
*breakpoint*
realm.addOrUpdate(object)
}
I add a before realm.addOrUpdate(object) and print the object, it show correct object, but after realm.addOrUpdate(object) get executed, in realm file, I can only see an object
{
id: 169e6bc2-9b34-44ae-8ac3-70e6b9145adc,
name: ""
}
and the id is also different from what I saw in break point. It looks like Realm create an object rather use the object I passed in. I am asking for some help here.
So what will cause realm create an empty object(maybe default value?) rather than save the object I passed. I just want to get some possible reasons here.
I think I got it, in my project, we have a category for NSObject which include a method called objectForKey, and in Realm's src, when we read value from RLMObject, we check if it can response to objectForKey, normally it should return false and keep executing following code to get the real value, but in my project, the code will return nil because it's not a dictionary.
So will close this

Trying to mutate array and object passed as parameters to an asynchronous function

So if you look at the code below you will see the following line fails with:
cannot use mutating member on immutable value: 'forecasts' is a 'let' constant
caused by forecasts.append(forecast). I have a service file which you see below called ForecastService and I am using the function to asynchronously grab JSON in my view controller from an API. I am trying to pass an object and an array of objects to the function which I then want to mutate on so I can use the updated values in my view controller to update my UI. I want to construct forecast objects and push them into the the array which I initialized in the view controller.
Any clarification on how I can get around this problem would be great.
import Foundation
import Alamofire
class ForecastService {
static let sharedInstance = ForecastService()
private init() {}
func downloadForecastDetails(forecast: Forecast, forecasts: [Forecast], completed: #escaping DownloadComplete) {
let url = URL(string: FORECAST_URL)!
Alamofire.request(url).responseJSON { response in
let result = response.result.value
if let dict = result as? Dictionary<String, AnyObject> {
if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
for obj in list {
let forecast = Forecast(weather: obj)
forecasts.append(forecast)
}
}
}
completed()
}
}
}
The problem is not the asynchronous block, but the mechanism of parameter forecasts. It's copied when you call the function, even if you declare it as var. Because Array is Swift is value type, it's the Swift's policy. (https://developer.apple.com/swift/blog/?id=10)
You can try to call forecasts.append outside the asynchronous block, it will still fail. So you need another way to pass a reference type (not value type) into the function.

How to write a function that takes a string and a generic type as parameters?

I'm using a library called Gloss to help parse JSON data. As a result I've created structs that are of type Glossy:
struct LoyaltyCard: Glossy {
let id: Int
init?(json: JSON) {
guard let __id: Int = "id" <~~ json
else { return nil }
}
I have many different Glossy structs and want to pass them into a function along with a string but I keep getting an error: " Cannot invoke 'getMemberInfo' with an argument list of type '(String, memberData: LoyaltyCard.Type)'", here is an abbreviated version of my function:
func getMemberInfo<T: Glossy> (memberDataRequest: String, memberData:T) {
let urlAccess = "\(baseURL)/api/\(memberDataRequest)"
///////code////////////
let data = object as! NSData
let jsonInfo: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.init(rawValue: 0))
let jsonArray = jsonInfo as! NSArray
if let dict = jsonArray[0] as? JSON //This means as type Gloss
{
let loyaltyID= LoyaltyCard(json: dict)
print(loyaltyID?.id)
}
}
Any ideas how to make this function work?
I'm inferring from your code sample and from your comments, that you don't necessarily want to pass a Glossy type to getMemberInfo, but rather that the key requirement is that you want to perform a network request and return a Glossy type.
While I get what you were trying to do, I would personally retire the generic approach, and just use a protocol extension. You end up with a method that can be called for any Glossy type. And if this protocol extension method returns a type Self, that will end up returning whatever Glossy type from which you happen to call it.
First, let's step back and be clear as to what the Glossy protocol might look like. At the very least, you'd have some failable initializer (plus whatever else your types needed):
protocol Glossy {
init?(json: [String: AnyObject])
}
(Note, I'm not using JSON type, but feel free if you want. I personally just use Swift collections for parsed JSON, but do whatever you want.)
I'd then define a static method in a protocol extension to perform the request. The following method uses NSURLSession, but if you use Alamofire or something else, the basic idea is the same:
extension Glossy {
static func performMemberRequest(memberDataRequest: String, completionHandler:(Self?, ErrorType?) -> ()) -> NSURLSessionTask {
let urlAccess = "\(baseURL)/api/\(memberDataRequest)"
let request = NSMutableURLRequest(URL: NSURL(string: urlAccess)!)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard let data = data where error == nil else {
completionHandler(nil, error)
return
}
do {
if let array = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [[String: AnyObject]],
let dictionary = array.first {
completionHandler(Self(json: dictionary), nil)
} else {
completionHandler(nil, GlossyError.InvalidJSONError)
}
} catch let parseError {
completionHandler(nil, parseError)
}
}
task.resume()
return task
}
}
Note, there are a few notable issues entailed in the above:
Network requests should always be performed asynchronously. So use asynchronous pattern like completionHandler rather than trying to return some object immediately.
If you're going to return anything, the only thing you should be returning is the NSURLSessionTask object so the caller has the option to capture that and cancel requests, should you want that functionality.
I changed the name of the method to be more descriptive and conform to Cocoa naming conventions.
As an aside, your code suggests that your API returned an array and you're just grabbing the first dictionary. That seems like a curious pattern, but I've followed that, above. If you really were returning an array, it strikes me that you did that because you contemplate a scenario where you could be returning multiple items. In that case, I would advise iterate through the whole array and have the completion handler return [Self]? (an array of Glossy objects) rather than just Self? (i.e. only the first one).
Furthermore, I wouldn't personally advise a structure that returns an array as the top level item. How does that web service report errors? I'd have a dictionary structure that returned success/failure and/or return code and the like. And then have a dedicated key for results which would be your array of results.
But I didn't tackle any of these broader API issues above, but rather followed the pattern in your code snippet. But these are considerations you might want to think about.
In my example, I didn't dispatch these completionHandler calls back to the main queue, but that's often a very useful pattern (avoids synchronization problems, UI updates, etc.). It's trivial to do, but I wanted to keep the above relatively simple.
But, let's step aside from the details of your API and the like. Let's focus on the notion that you want a static method defined in the protocol extension, (and it can therefore be called from any type that conforms to Glossy). For example, I can then define the LoyaltyCard class with the required initializer:
struct LoyaltyCard: Glossy {
let id: Int
init?(json: [String: AnyObject]) {
guard let id = json["id"] as? Int else {
return nil
}
self.id = id
}
}
Having done all that, I can now invoke the static method of Glossy protocol extension on LoyaltyCard, for example:
LoyaltyCard.performMemberRequest(memberDataRequest) { loyaltyCard, error in
guard let loyaltyCard = loyaltyCard where error == nil else {
print(error)
return
}
// do something with loyaltyCard here
print(loyaltyCard)
}
// but don't use it here
There's a lot there, but I don't want you to get lost in the details. But I do hope you grok the key concepts here: Don't pass a Glossy type to your method, nor use a generic: Instead use protocol extension. And avoid synchronous network requests, so instead use a asynchronous pattern like the completionHandler pattern.

Check for object type fails with "is not a type" error

I'm trying to check if an object is of a given type and I'm getting an error:
'expectedClass' is not a type
My code below.
func testInputValue(inputValue: AnyObject, isKindOfClass expectedClass: AnyClass) throws {
guard let object = inputValue as? expectedClass else {
// Throw an error
let userInfo = [NSLocalizedDescriptionKey: "Expected an inputValue of type \(expectedClass), but got a \(inputValue.dynamicType)"]
throw NSError(domain: RKValueTransformersErrorDomain, code: Int(RKValueTransformationError.UntransformableInputValue.rawValue), userInfo: userInfo)
}
}
I'm trying to figure out what can be wrong here.
You should be able to do this with generics:
func testInputValue<T>(inputValue: AnyObject, isKindOfClass expectedClass: T.Type) throws {
guard let object = inputValue as? T else {
...
}
}
You should not do class comparisons with == as suggested in one of the other answers, unless you specifically want to test if the type of the object tested should exactly match the expected class and it is not allowed to be a subclass of the tested class.
You can use the instance method isKindOfClass to accomplish this, taking subclassing into account. See below for a code example.
NOTE: You may be surprised that this works on a pure Swift class type, given an instance method with the same name exists in NSObject / NSObjectProtocol. It does indeed work in pure Swift (as shown with the example code below – tested with Xcode 7.3, Swift 2.2.1), with no #objc types involved, as long as you import Foundation. I am presuming based on this that this instance method is added as an extension method in Foundation to all class types.
import Foundation
class A { }
class B { }
class C: B { }
enum ValueTestError: ErrorType {
case UnexpectedClass(AnyClass)
}
func testInputValue(inputObj:AnyObject, isKindOfClass expectedClass:AnyClass) throws {
guard inputObj.isKindOfClass(expectedClass) else {
throw ValueTestError.UnexpectedClass(inputObj.dynamicType)
}
}
let a = A()
let b = B()
let c = C()
do {
try testInputValue(c, isKindOfClass: B.self)
} catch {
print("This does not get printed as c is of type B (C is subclass of B).")
}
do {
try testInputValue(a, isKindOfClass: B.self)
} catch {
print("This gets printed as a is not of type B.")
}
Also, importantly although isKindOfClass is available as an instance method on AnyObject, trying to call it on an arbitrary Swift class typed object will only work if you first cast that object to AnyObject (which will always of course succeed). Example code illustrating this point is presented below, and there's more on this question over here.
import Foundation
class A {}
let a = A()
// the following compiles and returns 'true'
(a as AnyObject).isKindOfClass(A.self)
// the following fails to compile with "Value of type 'A' has no member 'isKindOfClass'"
a.isKindOfClass(A.self)
Not the greatest answer ever, but comparing with inputValue.dynamicType works:
if inputValue.dynamicType == expectedClass {
print("inputValue.dynamicType \(inputValue.dynamicType) equals expectedClass \(expectedClass)")
// ...
}

Resources