Singleton implementation in Swift 2 no longer works for Swift 3 - singleton

This worked in Swift 2 but causes an error in Swift 3...
class SomeClass {
var Role: Int
var Status: Bool
static let sharedClass = SomeClass()
private init(){
Role = 3
Status = false
}
}
let shared = SomeClass() //error on this line -> SomeClass initializer is inaccesible due to private protection level
I can remove the private keyword from the init() but I am not sure if that is still a proper singleton implementation. Does anybody know what the right way to do this in Swift 3 is? Thanks

This line:
let shared = SomeClass()
isn't using the singleton. It's attempting to create a new instance of SomeClass. And it fails because the init is private.
You want:
let shared = SomeClass.sharedClass
That makes use of the singleton.

You are not supposed to use SomeClass() at all, that's the whole point: your singleton is only accessible via
SomeClass.sharedClass
and this is on purpose: the same instance is shared and used in different places.

Related

Bridge Class Functions in React Native

Say I have a class called ExampleClass.
Say I then write code like so:
#objc(ExampleClass)
class ExampleClass: NSObject {
#objc class func exampleFunc() -> Void {
}
}
With an Objective-C file header like so:
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#interface RCT_EXTERN_MODULE(ExampleClass, NSObject)
RCT_EXTERN_METHOD(exampleFunc)
#end
Which I then consume in my React Native app like so:
console.log('exampleClass', React.NativeModules.ExampleClass);
console.log('exampleFunc', React.NativeModules.ExampleClass.exampleFunc)
The first console log results in {exampleFunc: f}
The second results in undefined,
Calling the function: React.NativeModules.ExampleClass.exampleFunc() results in an app crash with:
Exception 'exampleFunc is not a recognized Objective-C method.' was thrown while invoking setupLogger on target ExampleClass with params (
While changing only the Swift so that it reads:
#objc(ExampleClass)
class ExampleClass: NSObject {
#obj func exampleFunc() -> Void {
}
}
results in calling the function (which, yes) does nothing at present.
How can I expose class level variables? I am trying to write functional Swift, and I am using class methods to simulate structs.
I believe the problem is that RCT_EXPORT_METHOD() only works on instance methods, not class methods, according to my own similar problem and some discussion here: https://github.com/facebook/react-native/issues/2311
My use case is trying to bridge a getInstance() method for a Swift singleton class. This is problematic because when you reference NativeModules.ExampleClass from javascript, which has been exported with RCT_EXTERN_MODULE(), RN calls init() on its own, which you don't want for a singleton (init() reference: https://samwize.com/2017/02/09/calling-a-view-controller-function-from-react-native/)
The best way I've found to accomplish this is pretty ugly. I have a dummy wrapper class that does nothing except call methods on the singleton, and this is the class I export to Objective C (and therefore to React Native). It's basically like this:
#objc(StupidWrapperClass)
class StupidWrapperClass : NSObject {
#objc(pseudoSingletonSomeMethod)
public func pseudoSingletonSomeMethod() {
let singleton = ActualClass.getInstance()
singleton.someMethod()
}
}
and then in the .m bridge file:
#interface RCT_EXTERN_MODULE(StupidWrapperClass, NSObject)
RCT_EXTERN_METHOD(pseudoSingletonSomeMethod)
#end
You could do something like this for a class method, too:
#objc(StupidWrapperClass)
class StupidWrapperClass : NSObject {
#objc(pseudoClassMethod)
public func pseudoClassMethod() {
ActualClass.theRealClassMethod()
}
}
I know I'm kinda late to the party but I recently faced the same problem and I fixed it using a different approach. Adding to the answer given above by #thejoelpatrol, a different approach would be to store the object's reference created by react native in some static variable that would be accessible by you. Then we can use the variable to access the object created by react-native anytime.
Whenever React Native tries to instantiate the class, it would come to the init. inside the init, we can save the reference to the object created by RN.
#objc public class MyClass {
#objc public static var shared: MyClass?
init() {
MyClass.shared = self
}
}
The .m bridge file is as follows:
#interface RCT_EXTERN_MODULE(MyClass)
RCT_EXTERN_METHOD(myClassMethod)
#end

Property 'sharedInstance' not found on object of type ClassA

I am creating a swift framework. In that one class is like this as shown below.
import Foundation
#objc public class classA: NSObject {
public override init (){
super.init();
}
/**
Singleton intance is returned.
*/
public class var sharedInstance: classA {
struct Static {
static let instance = popeye();
}
return Static.instance
}
}
Now when i add this framework into a Objective c project and try to access "sharedInstance" i get this error.
Property 'sharedInstance' not found on object of type ClassA.
Fix it Replace 'sharedInstance' with 'sharedInstance'
But even if i try use Fix it, this issue isnt solved.
NOTE: This issue doesn't happen when i integrate this framework with a swift project!!!
I AM STUCK.. :(
I tried to reproduce your problem. At first the syntax highlighter in Xcode flagged the same error in Objective-C that you mentioned, but the code actually was built and ran fine.
However, there is a cleaner way of doing this. In your code you are using a computed type property, which is evaluated every time you access it! You work around this by introducing the struct Static, where you essentially do what could be done in classA itself, like this:
/**
Singleton intance is returned.
*/
public static var sharedInstance: classA = popeye()
Here we used a stored type property, which is a recommended way to implement singletons, see here:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html
And here is some documentation on different kinds of properties:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html
Finally i was able to fix this with a minor change !! :)
Swift framework code
#objc class SingletonTest: NSObject {
// swiftSharedInstance is not accessible from ObjC
class var swiftSharedInstance: SingletonTest {
struct Singleton {
static let instance = SingletonTest()
}
return Singleton.instance
}
// the sharedInstance class method can be reached from ObjC
class func sharedInstance() -> SingletonTest {
return SingletonTest.swiftSharedInstance
}
// Some testing
func testTheSingleton() -> String {
return "Hello World"
}
}
Objective C parent project code
SingletonTest *aTest = [SingletonTest sharedInstance];
NSLog(#"Singleton says: %#", [aTest testTheSingleton]);

How to declare a property as a function in Swift?

Here is my code:
import Cocoa
class VC1: NSViewController {
let aFunctionVar ()->Void
}
The compiler however tells me: "Class VC1 has no initializers"
According to the swift example in Apple Swift iBook, they did their examplle like so:
var mathFunction: (Int, Int) -> Int = addTwoInts
But in my case, I'm trying to create a property variable. It is not yet known what the variable will be, so i can't set it there. Any help?
Edit - I already know how to make variables optional and lazy when it comes to simple String/Array/Dictionary types etc. But this is a function type property variable. It is meant to hold a function of type ()->Void. Any help on how this can be done?
In objectiveC this can be done by making a block property like this:
#property (nonatomic, copy) void (^aFunctionVar)();
Declare projectLaunchData as an optional var:
import Cocoa
class VC1: NSViewController {
var projectLaunchData: (()->Void)?
}
Then you can assign a value later:
func test() {
print("this works")
}
let myVC = VC1()
// assign the function
myVC.projectLaunchData = test
// Call the function using optional chaining. This will safely do nothing
// if projectLaunchData is nil, and call the function if it has been assigned.
// If the function returns a value, it will then be optional because it was
// called with the optional chaining syntax.
myVC.projectLaunchData?()
If the value will be known after the object is setup, you can use a lazy variable:
class LazyTester {
lazy var someLazyString: String = {
return "So sleepy"
}()
}
var myLazyTester = LazyTester()
myLazyTester.someLazyString
The compiler is giving you that error because you are defining a mandatory stored variable, projectLaunchData, but not giving it a value. If you know the variables value at init time, you can set it at init time.

Getting class from string in swift

I have created a custom class named PredicateController.
I need to pass the class of PredicateController which will be called from a framework written in Objc. However, whenever I am using the properties of swift, the same is returning me <ProjectName>.PredicateController.
I only need the class name as PredicateController.
Is there any workaround in swift to do the same.
Thanks.
You can get the type of a class by using self on the class and then simply wrap it into a string, e.g. ...
let date = NSDate();
let clazz = NSDate.self
let str = "\(date) is of type \(clazz)"
println(str)
You can use .className on the instance if the class is inheriting from NSObject:
class PredicateController: NSObject {
// ...
}
let pc = PredicateController()
let klass = pc.className
let name = klass.componentsSeparatedByString(".")[1]
let str = "\(pc) is of type \(name)"
println(str) // "<__lldb_expr_1398.PredicateController: 0x7fd92b465080> is of type PredicateController"
My answer is a follow-up to #hexagonstar's one. I don't know if it's the proper way of doing this, but it works.

Get a user-readable version of the class name in swift (in objc NSStringFromClass was fine)

Is there an equivalent of NSStringFromClass in Swift that gives a user-readable version of the class name? I've tried to use it with a native Swift class I created, but as you can see, the result seems to be the compiler's internal representation of the class name:
println(NSStringFromClass(MyClass.self))
Result:
_TtC5test7MyClass
I've tried adding the #objc attribute to the class, and making it a subclass of NSObject, but it makes no difference. I've discovered that if I replace MyClass with an Objective-C class of the same name, and import this in the bridging header, it gives me 'MyClass', but this shouldn't be necessary.
Another option would be to make a protocol for this, which each class I want to use in this way must conform to:
protocol Nameable {
class var className: String { get }
}
However, it would be easier to have a built-in way to do this in the language.
You can now just do:
String(MyClass)
new format based on xcode 6 beta 5.
(project_name).(class_name)
func getName(classType:AnyClass) -> String {
let classString = NSStringFromClass(classType.self)
let range = classString.rangeOfString(".", options: NSStringCompareOptions.CaseInsensitiveSearch, range: Range<String.Index>(start:classString.startIndex, end: classString.endIndex), locale: nil)
return classString.substringFromIndex(range!.endIndex)
}
Latest 6.3 Xcode Swift 1.2
if you need an extension or you can put this on any common object:
public extension NSObject{
public class var nameOfClass: String{
return NSStringFromClass(self).componentsSeparatedByString(".").last!
}
public var nameOfClass: String{
return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
}
}
Swift 3
type(of: self) prints ModuleName.ClassName
String(describing: type(of: self)) prints ClassName
At the moment, there's no reliable way to do this. See an Apple developer's comment on https://devforums.apple.com/thread/227425
Swift does not currently have much in the way of introspection.
There is some introspection machinery that is used for the
playgrounds. I don't know if that is intended to be API.
Some Swift
methods and variables can be examined using the Objective-C runtime's
introspection. That's likely to be the best solution today.
Swift does have the notion of a metatype, somewhat analogous to the Class type in Objective C. You can find it using TypeName.self, e.g.:
class Foo {
#required init() {
}
}
var normalFoo : Foo = Foo()
var fooType : Foo.Type = Foo.self;
var fooFromMetatype : Foo = fooType();
Perhaps, by release time, metatypes will include more introspection abilities. I suggest filing a Radar feature request for this.
In Swift 2 beta 4 you can get to the information via the type object:
let className = "\(String.self)" // gives: "Swift.String"
or if you have an instance:
let s = "Hello World"
let className = "\(s.dynamicType)" // gives: "Swift.String"
You get the Module.Class result, like:
Swift.String
ABC.MyGenericClass<Swift.Int>
Funny enough the Type object returned does not seem to conform to the CustomStringConvertible protocol. Hence it has no 'description' property, though the "" pattern still does the right thing.
P.S.: Before b4 the same could be accomplished via reflect(obj.dynamicType).summary.
In Swift v3.0, this worked for me:
String.init(describing: self.self)
----- Updated -----
As #ThomasW mentioned, for Swift 4, we need to use String(describing:type(of:self))
----- Old post ----
I prefer to use String(self.dynamicType)
Use it in my project https://github.com/JakeLin/SwiftWeather/blob/e7165b0919dda53fd7fcba9b43fdfe150d73c6f8/SwiftWeather/ForecastView.swift#L135
If you want to have only the name of the class in swift you can parse the string returned by NSStringFromClass().
This is done in nameOfClass.swift of the INSwift Github repository:
https://github.com/indieSoftware/INSwift
You shouls now be able to use the following to retrieve the class name in swift
let nameSpaceClassName = NSStringFromClass(RWTCountryPopoverViewController.self)
let className = nameSpaceClassName.componentsSeparatedByString(".").last! as String
This is a bit shorter. No need of NSStringFromClass
MyObject.self.description().componentsSeparatedByString(".").last!
Here is Swift 3+, Xcode 8+ example with code:
class MySuperbClass{
let a = 4
func getClassName() -> String? {
return String(describing: type(of:self)).components(separatedBy: ".").first
}
}
let className = String(describing: type(of:MySuperbClass.self)).components(separatedBy: ".").first
//className = "MySuperbClass"
let classNameFromObject = MySuperbClass().getClassName()
//classNameFromObject = "MySuperbClass"
Swift 4
super.init(nibName:String(describing:MyClass.self), bundle: nil)
myObject.description().componentsSeparatedByString(" ").first!
This is not exactly what you want - it will add an unwanted leading '<' and trailing ':'. But when I am debugging I value speed over neatness so this quick + dirty trick worked for me.
Swift 3
NSStringFromClass(self).components(separatedBy: ".").last!
In latest version of swift, below works for me:
NSStringFromClass(type(of: device!.self))