ios


In swift, when one property has no default value, the default values held by other properties become useless?


In swift, I have the following code:
struct Person {
var name: String
var nose: Int = 1
}
var mike = Person(name: "mike", nose: 1)
var john = Person(name: "john") //error: missing argument for parameter 'nose' in call
Although nose has a default value, I have to specify a value for it. My question is, does default value of nose become totally useless and I can simply write the declaration as the following?
struct Person {
var name: String
var nose: Int
}
Thanks!
From The Swift Programming Language
Structure types automatically receive a memberwise initializer if they
do not define any of their own custom initializers. This is true even
if the structure’s stored properties do not have default values.
The memberwise initializer is a shorthand way to initialize the member
properties of new structure instances. Initial values for the
properties of the new instance can be passed to the memberwise
initializer by name.
As you haven't declared any initialisers for your struct, the only initialiser that is available is the memberwise initialiser.
You can declare an initialiser that just takes a name in order to use the default value for nose. This then requires you to explicitly declare the memberwise initialiser -
struct Person {
var name: String
var nose: Int = 1
init(_ name:String) {
self.name=name;
}
init (name:String, nose:Int){
self.name=name;
self.nose=nose;
}
}
var mike = Person(name: "mike", nose: 1)
var john = Person("john")
I like this form because it is so flexible:
struct Person {
var name: String
var nose: Int
init(name: String = "Anon", nose: Int = 1) {
self.name=name;
self.nose=nose;
}
}
var sally = Person(nose: 2, name: "Sally")
var anon1 = Person(nose: 1)
var anon2 = Person()
var mike = Person(name: "mike", nose: 1)
var john = Person(name: "john")
Note 5 different initialiser combinations possible. Essentially it allows you to treat Person like a person builder (Builder pattern).

Related Links

Healthkit privacy policy, how?
CNContact unifiedContact broken with Mac OS Sierra
Constraint Warning with Dynamic Section Header
Application Loader getting stuck while uploading package to App Store
Xcode 8 with “Release: Fastest, Smallest [-Os]” have some weird issue and not functional well in some case
How to fix PageMenu on ViewController Swift
Strange border glitch of an ImageView
How to export code on Origami Studio
Accessing QBCOCustomObject retrieved from QBRequest inaccessible outside of QBRequest
use beacon wake up APP in the background to scan BLE - IOS
__block in Swift [closed]
UI Screen becomes black after UIAnimation
Best way to display tabular data like the attached screenshot in Swift?
UIView in UIScrollView won't stretch full width
Get a reference to the more navigation controller in UITabbbarController
How can I tell if my user upgraded to a new phone, or restored from a backup in iOS?

Categories

HOME
python-3.5
backand
prebuild
addeventlistener
go-gorm
wikipedia-api
mvvm-light
range
mobilefirst-adapters
hadoop2
android-vision
wso2ei
avplayer
uwsgi
fbx
pyfftw
partitioning
jgit
lazy-loading
servlet-filters
h.264
pwm
cadvisor
jsgrid
liferay-aui
digital-logic
iteration
scom
swipe
kube-dns
roo
decompiler
disqus
flags
visualsvn-server
jpype
logicblox
monogame
zfs
botkit
maven-jetty-plugin
psycopg2
website-hosting
dump
laravel-routing
html-lists
masm32
master-data-management
maven-versions-plugin
asp.net-mvc-custom-filter
breach-attack
geneticsharp
vs-2015-preview
surroundscm
perspective
finalcut
4d
groups
jstorm
sqlj
voting
translucency
wss4j
vraptor
mklink
bitrock
boxapiv2
ratingbar
publish-actions
django-supervisor
email-spam
cellular-automata
ituneslibrary
qt4.7
scala-pickling
flash-cs4
zephir
ccombobox
xcode4.2
mosek
pixelsense
file-structure
android-actionmode
zmodem
xml-dtd
ria
oledbcommand
hpple
ms-access-97
azure-role-environment
f2c
nstreecontroller
android-3.1
custom-tag
jboss-cache
regexbuddy
adbwireless
scramble
drupal-gmap
dynamic-websites
nhaml
variable-names
mud

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App