How possibility to run object globally that I call from Objective C library in swift? I don't want to call it more than once each time I switch the view controller. Is it I need to call it from appDelegate() ?
var instanceOfUMCrypto: UMCrypto = UMCrypto()
If you need a global singleton object, you can declare it globally in any .swift file (not in any class or function), like that:
import UIKit
let instanceOfUMCrypto = UMCrypto()
class SomeClass {
someFunction() {
}
}
After that, you can call instanceOfUMCrypto from anywhere and it won't be initialized more than once.
Related
I have a framework where I have a singleton class, let's say Singleton. This class is used by other classes in the framework.
In the app project I want to subclass this singleton class, e.g. AppSingleton: Singleton. Is it possible? What is the right solution?
I provide a solution but it may be a little hacky.
Class A {
open class var shared: A {
return A.privateShared
}
private static let privateShared = A()
}
Class B {
open class var shared: B {
return A.privateShared
}
private static let privateShared = B()
}
I must clarify, this ways isn't perfect since it actually create 2 instance! So, it will technically not a singleton any more.
However, you can override the class B's property or method to call A.shared method or property instead. You must know what you are doing and consider use the other way to fix the problem you want to solve.
I have an array stored in a class that downloads its objects from the internet. My class is set up like so:
class StockManager {
var managerStock: [Dictionary<String, String>] {
return downloadStockFromDatabase()
}
...
}
I access the managerStock from other Swift files in my project like so, but it always resorts to re-downloading the stock again no matter if I have used the variable before (ie. recalls the function downloadStockFromDatabase):
let stockManager = StockManager()
print(stockManager.managerStock)
How would I make sure the managerStock only downloads once and I could use it in any of my files?
This is a question of correct software pattern usage. I would suggest:
make StockManager a singleton, so you will always access the same instance of it
initialize it e.g. in the AppDelegate, i.e. make sure it stays alive for the whole runtime
tip: call managerStock lazily, i.e. only when you really need it and not as part of initialization
As ff10 and holex suggested, make your class a singleton. It will look like this:
class StockManager {
static let sharedInstance = StockManager ()
var managerStock: [Dictionary<String, String>] {
return downloadStockFromDatabase()
}
...
}
Then access it using the static sharedInstance property:
print(StockManager.sharedInstance.managerStock)
I learn the Swift Language and i need to create a manager like a Parse sdk.
For exemple when you initialize your Parse in app you write
Parse.setApplication("...", applicationId:"...")
And later you can write code like this
Parse.doSomething()
The method doSomething() use initial context.
Can you show me in my class should look like? I try some singleton exemple, but a have MyClass.sharedAttribute.doSomething() in case
What you have shown is no indication of singletons whatsoever, it sounds and looks more like a static class with static members and properties:
class MyStatic {
static var appIdA : String?
class func setApplicationId(a : String) {
appIdA = a
}
class func doSomething() {
print(appIdA)
}
}
MyStatic.setApplicationId("blabla")
MyStatic.doSomething() // prints Optional("blabla")
Of course there is the possibility that internally the class is a singleton, but Parse does not seem to be one, just looking at the functions it exposes.
The code comments even state
/*!
The `Parse` class contains static functions that handle global configuration
for the Parse framework.
*/
I am new to swift and I am trying to figure out how to add a function that could be usable throughout the entire project. A simple function like
func globalFunction() {
println("Global function!")
}
Then be able to call this function on any swift file within my project. Where would I declare this function?
Its just like in any programming language - declaring the function outside the class, something like:
class A
{
var a:Int
// you can call your global function here
}
class B
{
var b:Int
// and here
}
func flobalFunction()
{
println("Hello, I am a global function!")
}
Use static functions if you still want to bind the method to a class:
class myClass{
static func globalFunc() -> Void {
println("This is it")
}
}
myClass.globalFunc()
You can add a .swift file in your project and declare your functions. Functions declared in this file will be available in the same module.
By default, variables, constants, and other named declarations that
are declared at the top-level of a source file are accessible to code
in every source file that is part of the same module.
Taken from The Swift programming language, see section on Top-Level Code.
If it is a helper function, you can create a new Swift file, and place the function in it else place it in any class as:
Class HelperFunctionsClass() {
func anyFunction () {
//Your implementation of the function here
}
}
To use this, create an instance of the class in the place you would like to use it as:
var instanceOfHelperFunctionClass = HelperFunctionClass()
Now simply use this instance variable to use the function as:
insttanceOfHelperFunctionClass.anyFunction()
As simply using the sharedApplication() method is unavailable in WatchKit, utilizing the AppDelegate as a way to store data for all my interfaces/views is no longer an option in WatchKit. Is there an alternative method to do this in swift for WatchKit apps? I have tried making a singleton class like so:
import Foundation
import UIKit
class data: NSObject {
class var sharedInstance : data {
struct Example {
static let instance = data()
}
return Example.instance
}
var value:Int = 0
var increment:Int = 1
}
(This code is from another StackOverflow post, I just forgot the link.) I then try and access the Integer "value" by calling data.value in an interface controller but I get the error 'data.Type' does not have a member named 'value'. How can I fix this error? Or is there a better way to do what I am trying to achieve?
Thanks
The code data.value attempts to access a class variable or method on the class data called value. Since no such variable or method exists you get the error message.
In your definition value is defined as a property on the class data, so to use it you need a valid instance of data. Your sharedInstance method provides the instance.
Also, the convention in Swift is to capitalize class names, so I recommend using Data.