call methode from one class in another (swift) [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What I want to do is create a method in one class of my iOS app and then call that method from other classes in my app. Could someone explain exactly what I need to do to achieve this? Any help would be greatly appreciated as all my attempts so far have failed!
Thanks.

class Principle
{
var name:String = ""
func get_name() -> String
{
return self.name
}
}
class School
{
var princ:Principle = Principle()
init()
{
princ.name = "Mike"
println(princ.get_name())
}
}

class MyClass : OptionalSuperClass, OptionalProtocol1 {
var myProperty:String
var myOptionalProperty:String?
// More properties...
func doIt(a:Int) -> Int {
return a
}
var a = MyClass()
a.doIt(1)

Related

compression a compound Variable with swift [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 months ago.
Improve this question
Hello I wrote this little piece of code, but I have the impression that it is not optimal, indeed as the field variable is in get only : I can not directly change it .... but I am junior : so I would be delighted if someone has a better idea :) thank you .
let keyword = ["potatoes","garlic"]
var field: String {
var element = ""
keyword.forEach {
element += "&field=" + $0
}
return element
}
a shorter code coming from professionalswift developper :)
this would be better
let keyword = ["potatoes","garlic"]
var field: String {
return keyword.map { "&field=\($0)"}.joined()
}

Initiate Codable Model Class in swift [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is it possible to initiate object which is codable. So that I can add values into variables.
Ex: let obj = MyClass() for NSObject class.
Something like this.
Please help me.
Thanks in advance.
Yes, it is possible:
struct Person: Codable {
let name: String
let age: Int
}
let p = Person(name: "Robert", age: 30)
print(p.name)

i have simple firebase code error in the new xcode update [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
this method was writing with swift 3 in swift 4 I get error and I don't know where is the error
var CURRENT_USER: User? {
if let currentUser = Auth.auth().currentUser {
return currentUser
}
return nil
}
The error might have something to do with the optional type since it states that:
Cannot convert return expression of type 'User' to return type 'User?'
A good place to start would be to return an optional value by changing your code to
var CURRENT_USER: User? {
return Auth.auth().currentUser
}

How dynamically initialise object in Swift 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I use below line of code to allocate an Object(Suppose my Object name is Car) dynamically.
[self initliazieObject:[Car class]]
- (id)initliazieObject:(Class)model{
id record = [[model alloc] init];
return record;
}
How I can do this in swift 3.
Exactly as in Objective-C. Try this in a playground:
class Car : NSObject {}
func factory(type:NSObject.Type) -> NSObject {
return type.init()
}
let c = factory(type:Car.self)
print(type(of:c)) // Car
(We can get fancy and do clever things with generics or Self to specify the type of the returned object more precisely, but my goal in this code is simply to do things the dumb way, just like Objective-C.)

When do I want to use static, final, private in swift 2.2? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I kind of know when to use those different keywords, but can someone clarify for me when exactly do I want to use them, and how do they affect my apps during runtime?
static
You use a static method or property when you need some data without allocating new object, the most common use of static could be the singleton design pattern or when you have some helper class.
So for example in a function you can call:
let temp = Helper.staticMethod();
where
class Helper {
private let test1234 = false
private func privateMethod() {
}
private static func privateStaticMethod() -> Int {
return 0
}
static func staticMethod() -> Int {
self.privateMethod() // Error
self.test1234 // Error
self.test1234 // Error
Helper.privateStaticMethod() // OK
return 0
}
}
from staticMethod you can't have the access to NOT static method or property of the class Helper
final
It should be best practice declare all classes final and remove it just if you have to subclass them. Final provides you a compile time error if you try to subclass a final class and so this can help you to have a clean architecture without risking to create messy code and it help the readability of the code as well.
final class FinalClass {
}
class Sub1: FinalClass {
} //Error
class Base {
}
class SubBase: Base {
} //OK
private
A private class / property is not visible outside the scope.
It helps you to have a good encapsulation in your architecture.
class A {
private let test = 1
let test2 = 1
}
let classA = A()
classA.test //Error
classA.test2 //OK
I suggest reading the Swift documentation for better explanations:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html

Resources