Separation of Function Declaration and Definition in Swift

Separation of function declaration and definition in Swift

In swift, there is no separation of declaration vs implementation. This works like most other modern languages like Python. If you want to get a quick picture of your class, you should use code folding. Simply fold all your methods and functions. And unfold a method of you want to modify / work on it.

Apple libraries (CoreGraphics) seem to separate declaration and definition (Swift)

Apple separated these using special Apple-only tools. You can't recreate this.

The entire implementation of CGRect is in CoreGraphics/CGGeometry.h:

struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;

This is the ObjC equivalent to what the tools converted to Swift (or possibly someone hand coded, since these don't look like many of the other auto-generated Swift code):

struct CGRect {
var origin: CGPoint
var size: CGSize
}

If you mean how the various functions are implemented (like CGRectMake), no, those aren't available. I don't believe CoreGraphics is open source.

is there an equivalent of method declaration/definition separation for Objective-C messages?

The typical way to deal with this is as follows:

//in DoThings.h
@interface DoThings : NSObject {
//instance variables go here
}

//public methods go here
- (void) doAPublicThing;

//properties go here

@end


//in DoThings.m
@interface DoThings (Private)
- (void)doOneThing;
- (void)doAnotherThing;
- (void)stillOtherThings;
@end

@implementation DoThings

- (void) doAPublicThing {
[self doOneThing];
}

- (void) doOneThing {
[self doAnotherThing];
}

- (void) doAnotherThing {
[self stillOtherThings];
}

@end

Functions and returning values

So in Swift, a function that has no arrow has a return type of Void:

func funcWithNoReturnType() {
//I don't return anything, but I still can return to jump out of the function
}

This could be rewritten as:

func funcWithNoReturnType() -> Void {
//I don't return anything, but I still can return to jump out of the function
}

so in your case...

func deposit(amount : Double) {
balance += amount
}

your method deposit takes a single parameter of Type Double and this returns nothing, which is exactly why you do not see a return statement in your method declaration. This method is simply adding, or depositing more money into your account, where no return statement is needed.

However, onto your withdraw method:

func withdraw(amount : Double) -> Bool {
if balance > amount {
balance -= amount
return true
} else {
println("Insufficient funds")
return false
}
}

This method takes a single parameter of Type Double, and returns a Boolean. In regard to your withdraw method, if your balance is less than the amount you're trying to withdraw (amount), then that's not possible, which is why it returns false, but if you do have enough money in your account, it gracefully withdraws the money, and returns true, to act as if the operation was successful.

I hope this clears up a little bit of what you were confused on.

SwiftUI Calling functions from other class

Depending on what your going to do in that call there are options, ex:

Option 1

struct ContentView: View {

let variable = TempC()
init() {
variable.GetData()
}
var body: some View {
Text("Hello World")
}
}

Option 2

struct ContentView: View {

let variable = TempC()

var body: some View {
Text("Hello World")
.onAppear {
self.variable.GetData()
}
}
}

Similarly you can call it in .onTapGesture or any other, pass reference to your class instance during initialising, etc.

Static function variables in Swift

I don't think Swift supports static variable without having it attached to a class/struct. Try declaring a private struct with static variable.

func foo() -> Int {
struct Holder {
static var timesCalled = 0
}
Holder.timesCalled += 1
return Holder.timesCalled
}

7> foo()
$R0: Int = 1
8> foo()
$R1: Int = 2
9> foo()
$R2: Int = 3

What this line means in Swift?

It's called variadic arguments, explained here.

A variadic parameter accepts zero or more values of a specified type.
You use a variadic parameter to specify that the parameter can be
passed a varying number of input values when the function is called.
Write variadic parameters by inserting three period characters (...)
after the parameter’s type name.

Difference between declaring a variable in ios Swift?

Here is a simple explanation

var indexArray = NSMutableArray()

As the above, indexArray variable can be any one , String , Int , ....... You didn't specifically give any type for that variable.

var indexArray : NSMutableArray = NSMutableArray()

In here you specifically give that indexArray is a NSMutableArray

You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store. Write a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.

This example provides a type annotation for a variable called welcomeMessage, to indicate that the variable can store String values:

 var welcomeMessage: String

The colon in the declaration means “…of type…,” so the code above can be read as:

Declare a variable called welcomeMessage that is of type String.

The phrase “of type String” means “can store any String value.” Think of it as meaning “the type of thing” (or “the kind of thing”) that can be stored.

The welcomeMessage variable can now be set to any string value without error:

 welcomeMessage = "Hello" 

You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:

var red, green, blue: Double”

* Note *

It is rare that you need to write type annotations in practice. If you provide an initial value for a constant or variable at the point that it is defined, Swift can almost always infer the type to be used for that constant or variable, as described in Type Safety and Type Inference. In the welcomeMessage example above, no initial value is provided, and so the type of the welcomeMessage variable is specified with a type annotation rather than being inferred from an initial value.

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2
Prerelease).” iBooks. https://itun.es/us/k5SW7.l



Related Topics



Leave a reply



Submit