Error accessing function of class while converting Obj C project to Swift - objective-c

I added my swift class to the target while removing my header file of the same objective C class from the target but this error shows when I try and build my project. I can't attach an image right now but the error states: "Use of instance member 'url' on type 'ServerURLFactory'; did you mean to use a value of type 'ServerURLFactory' instead?"
let accessURL: NSURL = NSURL(string: "\(ServerURLFactory.url())/CygnetInstanceXMLServlet?cygnetId=\(idNumber)")!
print(accessURL)
Has anyone ran into a similar problem and how to fix this confusing bug? Its as if the program is still trying to call the Obj C function instead of explicitly calling the one in the Swift file.

You're calling .url() on ServerURLFactory itself as a type:
ServerURLFactory.url()
I guess you should instantiate the class first. Probably something like this, but it depends on how the class is implemented:
let factory = ServerURLFactory()
Then:
factory.url()

Related

Store a custom object (class in Swift) for access by an Objective-C method

I have created a class in Swift (which I'm learning) that I use to hold certain values as follows:
#objc public class Book: NSObject {
var title: String = ""
var date: Date = Date()
var sku: Float = 0
}
I would now like to store this class in the equivalent of a property that can be accessed by my Objective-C class.
I tried creating a variable in the View Controller where the class is used as follows:
var lastBookViewed = Book()
However, when I try to save the object to the property with the followng code, I get an error:
//create instance of book
let myBook = Book()
//gather information about book
lastBookViewed = myBook //THROWS COMPILER ERROR 'Use of Unresolved Identifier lastBookViewed
Is there a way to do this?
If easier, storing the object to a property in the Objective-C file would work as well but so far I have not been able to get the Objective-C file to recognize the Book class created in Swift although I put #objc before it as recommended.
Edit
When I try to create a property or variable for storing the custom swift object in a different Swift class (the View Controller class) in the same Swift file with the following syntax:
var lastBookViewed: Book
the compiler gives a warning for the VC class "yourVC has no initializers"
Go to your project’s general settings. Select the proper target for your app. Go to “Build Settings” and switch to “All”, instead of “Basic” which is the default. Here search for the “Packaging” section. Turn on “Defines Module”, by changing “No” to “Yes”.
When this is turned on we will now be able to use swift classes inside
of objective-c files.
Before leaving the “Build Settings” look for “Product Module Name” in the “Packaging” section. This will be important so make sure to take note of and copy the “Product Module Name” exactly.
Next go to an objective-c class where you would like to be able to have access to your swift class. In the implementation or .m file of this class import a header like this:
#import "MyProjectModuleName-Swift.h"
Here the file name of the import must be the exact Project Module Name from the build settings. Any objective-c file where you want access to your swift class should include this import.
Now it is worth mentioning one potential issue that may arise when using a swift class. Objective-c cannot read top-level swift classes. So if you go to use a method or variable from your swift class directly it will not be recognized. There are one simple solution to this issue. It’s to make your class public
#objc public class myClass

User Defaults migration from Obj C to Swift

I am working on an Update for my OS X app which was initially written in Obj-C.
The update has been re written in Swift
I am facing a strange problem of User Defaults handling.(Since User preferences must not be changed in update)
All the native type (like Bool, String) user preferences are working fine, but the the Class which was NSCoding compliant is not able to deserialse / Unarchive. It is giving an error :
Error :[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (KSPerson) for key (NS.objects); the class may be defined in source code or a library that is not linked
I made following try outs, but still not able to figure out the solution
Initially I thought that it was due to different class names(in
Swift its Person instead of KSPerson). But changing the class name (back to KSPerson) did not solve the problem
Inorder to make class more Objective C like I also tried prepending the class with #objc
Here is the code
let UD = NSUserDefaults.standardUserDefaults()
func serialize() {
// Info: personList: [KSPerson]
let encodedObject: NSData = NSKeyedArchiver.archivedDataWithRootObject(personList)
UD.setObject(encodedObject, forKey: "key1")
print("Serialization complete")
}
func deserialise() {
let encodedObject: NSData = UD.objectForKey("key1") as! NSData
let personList: [KSUrlObject] = NSKeyedUnarchiver.unarchiveObjectWithData(encodedObject) as! [KSPerson]
for person in personList {
print(person)
}
Note:
I created a duplicate copy of Objective C code, and it deserialised ( what was serialised by original copy) perfectly.
To my surprise. when I rename the class KSPerson to JSPerson in duplicate copy it gave me the exact same error as seen above
So one thing is clear. You need to have same class name to Unarchive NSCoding compliant objects. But this is clearly not sufficient for Swift
Swift classes include their module name. Objective-C classes do not. So when you unarchive the Objective-C class "KSPerson", Swift looks through all its classes and finds "mygreatapp.KSPerson" and concludes they don't match.
You need to tell the unarchiver how to map the archived names to your module's class names. You can do this centrally using setClass(_:forClassName:):
NSKeyedUnarchiver.setClass(KSPerson.self, forClassName: "KSPerson")
This is a global registration that applies to all unarchivers that don't override it. You of course can register multiple names for the same class for unarchiving purposes. For example:
NSKeyedUnarchiver.setClass(KSPerson.self, forClassName: "KSPerson")
NSKeyedUnarchiver.setClass(KSPerson.self, forClassName: "mygreatapp.Person")
NSKeyedUnarchiver.setClass(KSPerson.self, forClassName: "TheOldPersonClassName")
When you write Swift archives, by default it will include the module name. This means that if you ever change module names (or archive in one module and unarchive in another), you won't be able to unarchive the data. There are some benefits to that (it can protect against accidentally unarchiving as the wrong type), but in many cases you may not want this. If not, you can override it by registering the mapping in the other direction:
NSKeyedArchiver.setClassName("KSPerson", forClass:KSPerson.self)
This only impacts classes archived after the registration. It won't modify existing archives.
This still is pretty fragile. unarchiveObjectWithData: raises an ObjC exception when it has a problem, which Swift can't catch. You're going to crash on bad data. You can avoid that using -decodeObjectForKey rather than +unarchiveObjectWithData:
let encodedObject: NSData = UD.objectForKey("key1") as? NSData ?? NSData()
let unarchiver = NSKeyedUnarchiver(forReadingWithData: encodedObject)
let personList : [KSPerson] = unarchiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as? [KSPerson] ?? []
This returns [] if there's a problem rather than crashing. It still respects the global classname registrations.

Strange Swift behaviour when using objc_getAssociatedObject()

The behaviour can be observed by placing this code in a playground:
import Foundation
import ObjectiveC
class TestClass {}
var obj = TestClass()
let stringValue = "xyz"
let key = "def"
objc_setAssociatedObject(obj, key, stringValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
//let f = "f"
let returnedString = objc_getAssociatedObject(obj, key)
This works and returns "xyz" from the objc_getAssociatedObject call.
However, if you remove the comment from the let f = "f" line. The objc_getAssociatedObject call now returns nil.
I'm baffled as to how setting a totally unrelated variable can effect the call.
Any ideas?
Looks like a bug.
The objc_... methods are part of the Objective-C runtime. They shouldn't exist in Swift.
That said they clearly do. So my guess is that there's something happening when you set that method that kicks the runtime, similar to calling synchronize on NSUserDefaults.
Edit: This NSHipster article explains that the ObjC runtime is actually there.
Edit 2: I tried a few experiments, and I'll make your question even weirder. Wrapping the test case inside the object yields the same result. But changing the variable name to an underscore fixes the problem:
let _ = "f"
I bet assigning a variable overwrites whatever runtime associations you set manually. The underscore just tells the compiler that you aren't using the result of the assignment.

How to change the namespace of a Swift class?

When you implement a class MyGreatClass in Swift its fully qualified name will by <MyPackageName>.MyGreatClass. This is different to Objective-C, where the fully qualified name of that same class is MyGreatClass.
Unfortunately this introduces a problem for me. When I am using NSUnarchiver and the archive was written with Objective-C objects I cannot unpack it with Swift-classes(see below for a detailed description).
This means I need to find a way to rename the namespace for my Swift classes. How do I do that?
Any help would be great!
Background: Why can't NSUnarchiver see/load my swift class?
I have implemented a small program to read a file, which was archived with NSArchive.
These are my files:
main.swift:
import Foundation
// parse command line - total path to *.trace file (from Apple's Instruments app)
var traceFilePath = Process.arguments[1]
var traceFile = NSURL(fileURLWithPath: traceFilePath)
var error:NSError?
// check if the file exists
if (traceFile?.checkResourceIsReachableAndReturnError(&error) == false){
// file does not exist or cannot be accessed
println("\(error)")
exit(1)
}
var rawData = NSData(contentsOfURL: traceFile!)
var data = NSUnarchiver(forReadingWithData: rawData!)
var decodedObject: AnyObject? = data?.decodeObject()
XRObjectAllocRun.swift:
import Foundation
class XRObjectAllocRun: NSObject {
// class to be implemented
}
When I now run my application on an Instruments-file I am getting the following error: Terminating app due to uncaught exception 'NSArchiverArchiveInconsistency', reason: '*** class error for 'XRObjectAllocRun': class not loaded'.
This is really strange because when I add the exact same class in an Objective-C file with a bridging header file I have no issues.
trace file reader-Bridging-Header.h: is empty.
XRObjectAllocRun.h:
#import <Foundation/Foundation.h>
#interface XRObjectAllocRun : NSObject
#end
XRObjectAllocRun.m:
#import "XRObjectAllocRun.h"
#implementation XRObjectAllocRun
#end
What am I missing? Why is my Objective-C class found, whereas my Swift class is not?
Swift has no issues for example with var x = XRObjectAllocRun() in main.swift, but yet the NSUnarchiver still complaints about a missing XRObjectAllocRun class when I stay purely within Swift. Is the NSUnarchiver looking in the wrong places - does it for some reason only accept Objective-C classes?
If you want to know what I am trying to do check this stackoverflow question out.
Update
This is what apple writes:
Swift classes are namespaced based on the module they are compiled in, even when used from Objective-C code. Unlike Objective-C, where all classes are part of a global namespace
Further more:
For example, when you create a document–based Mac app, you provide the name of your NSDocument subclass in your app’s Info.plist file. In Swift, you must use the full name of your document subclass, including the module name derived from the name of your app or framework.
Yikes, trying to figure out the mess now...
Try this when you declare your class:
#objc(XRObjectAllocRun) class XRObjectAllocRun: NSObject {
// class to be implemented
}
That will give this class the same name as the archived class, namely XRObjectAllocRun, instead of the namespaced Swift name trace_file_reader.XRObjectAllocRun.
This is always a concern when you're translating from Objective-C to Swift and you've got an existing archive to deal with. See Apple's documentation:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html
Note the discussion under "Exposing Swift Interfaces in Objective-C".

Problem calling class methods (Q:1)

I'm trying to convert an old 'C' program containing some static methods into Obj-c but I'm having a few problems getting it to compile. In the header file I've got:
#interface Anneal : NSObject
...
...
+(float)approxInitT;
-(costType)simulatedAnnealing;
...
and in the implementation file, the two problem methods (also cut-down for brevity):
#implementation Anneal
+(float)approxInitT
{
float T=0.0;
int m2=0;
...
if(m2==0)
T = T_LIFESAVER;
else
T = T / m2 / log(initProb);
return T;
}
-(costType)simulatedAnnealing
{
float T;
...
if(Tset)
T=initialT;
else
T=[self approxInitT]; // error:incompatible types in assignment
}
Unfortunately I'm getting an "incompatible types in assignment" error even though 'T' and the return from the class method are both of type 'float'. While the code contains multiple source files (from which I'm expecting to hit a few more problems in the next few days), they're both in the same one.
The problem is obviously caused by an error in the way I'm calling 'approxInitT()' but a search of the internet hasn't uncovered any answers to my prob so far.
As a novice I don't have any experience in multi-model code OR using static/class methods, and I'd sure appreciate any help with this. Thanks in advance :-)
Class methods donot belong to any particular instance of a class. So, try passing the message to class itself -
T = [ Anneal approxInitT ];
self references an instance of a particular class, but as you are calling a class method (+approxInitT), you must send the message to your class: T=[Anneal
approxInitT]