Use class func on class parameter in Swift - swift

I am trying to use a class func to set a title for a book, however it's not working. Please see my code below:
import Foundation
class Book: NSObject {
var bookTitle: String = ""
var bookPage: String = ""
override init(){
print("Book object has been created")
}
class func setPageTitle(title: String)
{
bookTitle = title //I get the error here
}
}
I want to make it mandatory to set a pageTitle when a Book object is created.
Can someone please help me ?

The best way to set any required property/attribute is when you initialize it. So try coding your class this way:
class Book: NSObject {
var bookTitle: String = ""
var bookPage: String = ""
init(title:String) {
bookTitle = title
print("Book object has been created")
}
}
Several notes:
Your error is because you declare class in your setPageTitle function. That makes no sense.
There are better (and other) ways to set the bookTitle, including after initialization. But you specifically wanted to make sure you have a title when initializing, so there you go.
There are definitely better ways to maintain the bookTitle attribute. (Most languages teach you to hold a price variable to start with.) I'm mostly trying to give you a way to initialize it with the class.
You probably don't need (or have) any superclass call to make, but you also may not need to make your Book class a NSObject either.

Related

How to init a struct in swift 4.2

What I want:
To initialise a struct before use. But I really don't know how, even after a lot of trying and searching.
Example of the struct:
struct Person: Decodable {
let name: String
let age: Int
}
I want to use this struct global in my script. The way I think it would have been is like this (but it does not work, because of line 3):
import UIKit
class ViewController: UIViewController {
let personExample: Person()
func configure(person: Person) {
self.personExample = person
}
override func viewDidLoad() {
super.viewDidLoad()
print(self.personExample)
}
}
The syntax here is simply incorrect:
let personExample: Person()
What you meant was this:
let personExample: Person
Person() is the result of calling init(), which is a value, not a type, and isn't a valid initializer for this type anyway. Person is the type, which is what you want for a declaration.
However, what you've written here doesn't quite line up with the description. If you want a "global," this is actually an instance variable. What you may have meant here is the following:
var personExample = Person(name: "Bob", age: 21)
That would assign personExample to that value (and through type inference, assign the type to Person). I've used var here because configure(person:) tries to modify it.
You also may be confused about initialization of instance variables in view controllers when using storyboards. If that's your issue (for example, you're seeing errors about personExample not being set during initialization), you'll need to explain a little more about your situation and we can help you with that.
You need
var personExample: Person?
func configure(person: Person) {
self.personExample = person
}
first this let personExample: Person() is an incorrect syntax and if you changed to
var personExample: Person
it'll raise an error as it must be initated in the vc init methods , also if you don't implement coding , remove : Decodable {

swift singleton with parameters

I'm relatively new to swift and searched around but could not find any satisfactory answer to my problem. I would like to have a Singleton class instance which can be initialized with some variables. E.g.
public class Singleton {
var car: String
var bus: String
init(car: String, bus: String) {
self.car = car
self.car = bus
}
func drive() {
print("I will drive")
}
}
public class SingletonConsumer {
// create an instance of Singleton Once
var driver: Singleton = Singleton(car: "honda", bus: "volvo")
driver.drive()
}
public class driverClassWorld : SingletonConsumer {
driver.drive()
}
how can i achieve it? I tried protocol but issue i am hitting is how to instantiate singleton class with parameters.
I don't get this problem?
First remove singleton from your brain for a moment. Because I think you have the wrong idea on what a singleton is.
Now lets rephrase your question to: "How to instantiate a class with parameter"
Its like this:
import Foundation
class Test {
let someText : String!
init(something:String){
someText = something
}
func test(){
print("TEST \(someText)")
}
}
let a = Test(something: "Me")
a.test()
Output:
TEST Optional("Me")
You just need to define a init with the parameters you want.
Now to properly instantiate a singleton (basically it just the class above but single instance). There are a lot of ways, the old Objective C approach is still valid.
But for swift this is the most common pattern. You need to define a Static Property.
Example:
import Foundation
class Test {
static let shared = Test(something: "REAL SINGLETON")
let someText : String!
init(something:String){
someText = something
}
func test(){
print("TEST \(someText)")
}
}
Test.shared.test()
Output:
TEST Optional("REAL SINGLETON")
Now reread the definition of a singleton:
a singleton class is a class that can have only one object (an
instance of the class) at a time
For other patterns in declaring a singleton:
https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift
Now, you might wonder: When does this singleton instance instantiated?
Answer: It is when it is first used/called.

How to convert String to Struct (in Swift)

I have a struct like,
struct LoginConstants {
struct Selectors {
let testa = "test1234"
}
}
and a class like,
class Login: XCTestCase {
override class func setUp () {
// below constant will have a value like "LoginConstants"
let localConstants = "\(String(describing: self))Constants"
}
}
... so here I have a struct-name as a string format in localConstants.
My Question is how I can access the LoginConstants properties from the localConstants string?
NOTE:
I know I can access the LoginConstants() directly.
But I am planning to create a parent class where I can access this ***Constants struct dynamically.
Thanks for the help!
Objective-C has the ability to do this, but Swift does not. If you give a class an Objective-C name via the #objc attribute, you can use the Objective-C runtime functions to access it by name. However, this is not possible with a struct.
It's probably not the best way to go anyway. A better solution is to rethink what you are trying to do, and access the struct type directly rather than by name.

Using a string parameter to describe class property

I want to write a function that takes a string and then prints the value of the class property with that name. In practice, there would be more than one property to choose form. For example...
class Apple{
var juiciness : Int = 0
init(juiciness: Int){
self.juiciness = juiciness
}
}
var myApple(juiciness : 10)
func printValue(property : String){
print(Apple.property) // <-- I want to use the string to choose a property
}
Obviously, I can't do this code but I know there has to be a better solution than just I series of if statements.
Apple has done this for you. It is known as key-value observing(KVO).
Try the following code in the playground:
let label = UILabel()
print(label.value(forKey: "font"))
Your own class can support KVO by inheriting from NSObject:
class YourClass: NSObject{ ... }

Differences between "static var" and "var" in Swift

What is the main difference between "static var" and "var" in Swift? Can someone explain this difference to me, possibly with a little example?
static var belongs to type itself while var belongs to instance (specific value that is of specific type) of type. For example:
struct Car {
static var numberOfWheels = 4
var plateNumber: String
}
Car.numberOfWheels = 3
let myCar = Car(plateNumber: "123456")
All cars has same amount of wheels. An you change it on type Car itself.
In order to change plate number you need to have instance of Car. For example, myCar.
I'll give you a very nice Swifty example based on this post. Though this is a bit more sophisticated.
Imagine you have a project in which you have 15 collectionViews in your app. For each you have to set the cellIdentifier & nibName. Do you really want to rewrite all code for your that 15 times?
There is a very POP solution to your problem:
Let's help ourselves by writing a protocol which returns a string version of our ClassName
protocol ReusableView: class {
static var defaultReuseIdentifier: String { get }
}
extension ReusableView where Self: UIView {
static var defaultReuseIdentifier: String {
return String(Self)
}
}
extension BookCell : ReusableView{
}
The same for the nibName of each custom cell you have created:
protocol NibLoadableView: class {
static var nibName: String { get }
}
extension NibLoadableView where Self: UIView {
static var nibName: String {
return String(Self)
}
}
extension BookCell: NibLoadableView {
}
so now where ever I need nibName I would just do
BookCell.nibName
And where ever I need cellIdentifier I would just do:
BookCell.defaultReuseIdentifier
Now specifically to your question. Do you think we need to change the cellIdentifier per each new instance of BookCell?! No! All cells of BookCell will have the same identifier. It's not something that would change per instance. As a result it's been made static
While I did answer your question, the solution to reducing the number of lines for the 15 collectionViews can still be significantly improved so do see the blog post linked.
That blog post has actually been turned into a video by NatashaTheRobot
A static var is property variable on a struct versus an instance of the struct. Note that static var can exist for an enum too.
Example:
struct MyStruct {
static var foo:Int = 0
var bar:Int
}
println("MyStruct.foo = \(MyStruct.foo)") // Prints out 0
MyStruct.foo = 10
println("MyStruct.foo = \(MyStruct.foo)") // Prints out 10
var myStructInstance = MyStruct(bar:12)
// bar is not
// println("MyStruct.bar = \(MyStruct.bar)")
println("myStructInstance = \(myStructInstance.bar)") // Prints out 12
Notice the difference? bar is defined on an instance of the struct. Whereas foo is defined on the struct itself.