How to make function that some parameters not required in when call it in iOS Swift 3?
You just have to make them nilable.
func myMethod(name name: String? = nil, age age: Int? = nil) {
print(name!)
}
Notice: when yo make parameters optional, you have to be careful about how to unwrap them. usually using if let
syntax is helpful.
func myMethod(name name: String? = nil, age age: Int? = nil) {
if let name = name {
print(name)
}
}
you can also provide default value for them:
func myMethod(name name: String? = "Donald", age age: Int? = 10) {
print(name!)
}
Default optional parameter in Swift function
Optionals and default parameters are two different things.
An Optional is a variable that can be nil
, that's it.
Default parameters use a default value when you omit that parameter, this default value is specified like this: func test(param: Int = 0)
If you specify a parameter that is an optional, you have to provide it, even if the value you want to pass is nil
. If your function looks like this func test(param: Int?)
, you can't call it like this test()
. Even though the parameter is optional, it doesn't have a default value.
You can also combine the two and have a parameter that takes an optional where nil
is the default value, like this: func test(param: Int? = nil)
.
How to pass a function as an optional parameter Swift
You can easily define a default value for a function as parameter:
func foo(completion: () -> Void = { }) {
completion()
}
You could also have it be nil by default:
func foo(completion: (() -> Void)? = nil) {
When it's an optional closure, you'll have to call it like this:
completion?()
swift class as parameter without .self
Coincidentally, this just came up on swift-evolution. The ability to omit .self
when the Type is the only argument, has been reported as a bug in Swift.
The relevant quote from Apple:
It's a bug.
.self
is supposed to be required everywhere.
Unfortunately, we've had the bug for a while, so I'm not sure how much
code we'd break by changing it. If we wanted to remove the
requirement, that would be considered a language change and would have
to go through the Swift Evolution Process.
Swift function containing parameters not available in Objective-C class
The bad boy in your parameter list is the Int?
param. Int
's are represented as NSInteger
in Objective-C. Thus they don't have a pointer and can't have a null
value. Consider removing the optional qualifier or changing it to NSNumber?
,
like: func getSearchResults(searchText: String?, searchType: String?, zoom: Int) -> NSDictionary
Swift: Why necessary to omit name of first parameter, but not subsequent parameters in function calls
Because the naming convention for Objective-C (see here) and Swift is to end your method name by the name of your first argument:
func greetPersonNamed(name: String, onDay day: String) -> String {
return "Hello \(name), today is \(day)."
}
greetPersonNamed("Anna", onDay: "Tuesday")
If you prefer to write the name of the first argument, you can do it like so:
func greet(name name: String, day: String) -> String { /* ... */ }
greet(name: "Anna", day: "Tuesday")
The first name
refers to the external name, the second one is the one used inside your method.
EDIT
The naming guidelines for Swift 3 have been released (see here) and they differ from the ones used with Objective-C.
The name of the first argument should not be included in the method name. The external name of the first parameter can be omitted if the function intent is clear. Otherwise you should name it.
Let's say you can greet persons and pets. In that case, you should add an external name for the first argument:
func greet(person name: String, day: String)
func greet(pet name: String, day: String)
greet(person: "Anna", day: "Tuesday")
If you can only greet persons, then you can ommit it:
func greet(name: String, day: String)
greet("Anna", day: "Tuesday")
Optional Default Parameter in Swift
You'll have to use Optional strings String?
as your argument type, with default values of nil
. Then, when you call your function, you can supply a string or leave that argument out.
func test(string: String? = nil, middleString: String? = nil, endString: String? = nil) -> Void {
let s = string ?? ""
let mS = middleString ?? ""
let eS = endString ?? ""
// do stuff with s, mS, and eS, which are all guaranteed to be Strings
}
Inside your function, you'll have to check each argument for nil
and replace with a default value there. Using the ??
operator makes this easy.
You can then call your function by supplying all arguments, no arguments, or only the ones you want to include:
test(string: "foo", middleString: "bar", endString: "baz")
test()
test(string: "hello", endString: "world")
Inout parameters in swift 3
By moving s
into s1
you are making a copy of it since String is a struct, and all structs are pass by value. So in the end you're only changing s1
and not the passed in string, s
. Just remove it:
func removeFromString( _ s: inout String, Character c:Character) -> Int {
var nRemoved = 0
while let ix = s.characters.index(of: c) {
s.removeSubrange(ix...ix)
nRemoved += 1
}
return nRemoved
}
Related Topics
Xcodebuild -Exportarchive Wont Allow Me to Specify Filename
Delegate Property with Different Type in Swift
Applewatch Messages Url Works Hard Coded But Not with Variables
How to Customize the Title/Subtitle Font in Callout from Mkannotationview or Just Hide Them
Notification Extension Access Core Data
Cannot Load Module Coredata as Coredata
iOS 10 Notification Content Extension Not Loading
Why Does This Swiftui Picker Code Not Work
Xcode 8 Swift 3 Pitch-Altering Sounds
How to Open Your App's Settings (Inside the Settings App) with Swift (iOS 11)
How to Fix Error: Abort Trap 6 (In Target 'Realmswift' from Project 'Pods')
Swift - Associated Types in Protocol with Where Clause
Insert a Comment Using the Youtube API and Alamofire
How to Retrieve All Available Finder Tags
Reading Data from Excel Document in a Swift App
How to Set Alignment for Wkinterface Label Using Setattributedtext