Can we use get instead of function in swift? [duplicate] - swift

This question already has answers here:
Computed read-only property vs function in Swift
(11 answers)
Closed 1 year ago.
I have created getName1 & getName2() as below
var myName = "Jhon"
var salt = " Salt"
var getName1:String {
get {
return myName + salt
}
}
func getName2() ->String {
return myName + salt
}
print("getName1\(getName1)")
print("getName2\(getName2())")
So instead of getName2() can I use getName1 directly does that make any difference ?

There is no difference but getName1 is a computed property while getName2 is a function , in this case you better use getName1 as a function is expected to do some block code or IMO not just a one line

Related

Swift adding object to array appends a copy instead of a reference? [duplicate]

This question already has answers here:
Is Swift Pass By Value or Pass By Reference
(10 answers)
Closed 1 year ago.
Sample code:
struct test {
var itest: Int?;
}
var tests : [test] = [];
var t = test();
tests.append(t)
t.itest = 99;
print(tests[0].itest)
print(t.itest)
produces
nil
Optional(99)
So it seems the append command creates a copy of t and not its reference.
If the array would contain a reference of t, both print outs must be itest=99.
A struct is not an "object" in the way you are probably thinking. It's a value. When you assign a value, it's copied. That's by design.

Reading FireDatabase in Swift, while calling function [duplicate]

This question already has answers here:
Finish all asynchronous requests before loading data?
(6 answers)
Make code with Firebase asynchronous
(1 answer)
Swift Function returning a value from asynchronous firebase call
(2 answers)
Wait for Firebase to load before returning from a function
(2 answers)
Firebase with Swift 3 counting the number of children
(3 answers)
Closed 2 years ago.
func callData() -> String? {
var name: String = ""
var ref: DatabaseReference!
ref = Database.database().reference()
ref.child("reyddatbase").child("MovieData").child("InitialMovieData").child("Ariel").child("movie_name").observeSingleEvent(of: .value, with: { (snapshot) in
name = ((snapshot.value) as! NSString) as String;
print("\(name)") // prints what I need
})
print("\(name)") // prints nothing??
return "\(name)" // returns nothing?
}
I am attempting to read data from a database based in Firebase.
I would like to simply display the text on screen which I can do from content view.
However the print statement inside the ref.child shows my title in console, while my print statement outside of it remains an empty string.
I am not sure why the different print statements do not yield the same result.
I am very new to swift and have taught myself to this point.
I presume there is a simple solution to my problem. Thank you in advance for the help!

I am confuse structure vs class in swift language [duplicate]

This question already has answers here:
structure vs class in swift language
(13 answers)
Closed 3 years ago.
I am confused about structure vs class. I have seen this example According to this example structure-vs-class According to link may be code output is 15,15,15,20
BUT code output is
because Structure is not changed but when I have run code on Xcode it returns me 15,15,20,20
class objectmanagement
{
public var x : Int = 10;
func display()
{
print("\(x)")
}
}
struct StuctManagement{
var obj = objectmanagement()
}
let SA = StuctManagement()
SA.obj.x = 15
var SB = StuctManagement()
SB.obj = SA.obj
SA.obj.display()
SB.obj.display()
SB.obj.x = 20
SA.obj.display()
SB.obj.display()
I am confused please help me to understand this output this is same as a class output
In swift class is reference type. (see here)
When you say:
SB.obj = SA.obj
It means that the object of SA is the exact object in SB. (there is one pointer for SA.obj and SB.obj)
Although
let SB = SA
makes copy of SA and create SB with different reference.

Fixing a bug in a ternary-like if-else block [duplicate]

This question already has answers here:
Change in Xcode 10 Playgrounds Variable Initialization change? Are Xcode 10 Playgrounds an interpreter?
(2 answers)
Closed 4 years ago.
Instead of using the ternary operator, I coded a if-else block which doesn't seem to compile... Didn't understand the bug exactly, why isn't it working? Error line is :
error: MyPlayground2.playground:6:1: error: variables currently must have an initial value when entered at the top level of the REPL
var parentAge: Int
And my code:
import UIKit
var parent: String = "mom"
var parentAge: Int
let optOne = 39
let optTwo = 43
if parent == "mom" {
parentAge = optOne
print(parentAge)
} else {
parentAge = optTwo
print(parentAge)
}
error: variables currently must have an initial value when entered at the top level of the REPL
You should either initialise it with optional or assign an initial value if not optional.
var parentAge: Int?
var parentAge: Int = 0
Both of above representation are correct.

Print contents of array with custom class swift [duplicate]

This question already has answers here:
Overriding description method in NSObject on swift
(1 answer)
How can I change the textual representation displayed for a type in Swift?
(7 answers)
Closed 5 years ago.
I have an array of type custom class as seen below. It has strings, ints, and double values.
class TrendData: NSObject {
var earlyTime = Date()
var recentTime = Date()
var earlyTimePrice = Double()
var recentTimePrice = Double()
}
I have an array as follows
let dataArray = [TrendData]()
I have filled in 2 values into the dataArray.
Right now when I use a print command as follows
print ("Label: \(dataArray)")
it prints this Label: [<App.TrendData: 0x600000472440>] ** [<App.TrendData: 0x600000472440>]
This is the correct behavior but I want to see all the values within each of these elements in the array. What is the best way to do that? I don't want to explicitly list out each element or even put it into a loop if I can avoid it. I am able to do it, it is just really messy right now. Is there any function or command that does this automatically?