UserDefaults
- Numerous ways to make data persists in iOS:
FileManager
filesystemCoreData
SQL databaseCloudKit
database in the cloud- 3rd party options
UserDefaults
simplest to use but only for lightweight data. Like dictionary.
UserDefaults
is most often used by making one instance and using it everywherelet defaults = UserDefaults.standard
defaults.set(object, forKey: "SomeKey")
let i: Int = defaults.integer(forKey: "MyInteger")
let b: Data? = defaults.data(forKey: "MyData")
let a = array(forKey: "MyArray")
this returnsArray<Any>
- To use
Any
type it's possible to change the type withas
- To use
Gestures (8:37)
myView.gesture(theGesture)
- gesture is implemented in func or computer var
vartheGesture:someGesture{returnTapGesture(count:2).onEnded{}}
non-discrete gestures
To see end result
vartheGesture:someGesture{DragGesture(...).onEnded{valuein...}}
To see the value of gesture
@GestureStatevarmyGestureState:MyGestureStateType=<startingvalue>
Returns<starting value>
always when the gesture ends.
vartheGesture:someGesture{DragGesture(...).updating($myGestureState){value,myGestureState,transactioninmyGestureState=/* usually something related to value */}.onEnded{valuein/* do something */}}
myGestureState
can be modified only inside.updating
vartheGesture:someGesture{DragGesture(...).onChanged{valuein/* do something with value (which is the state of the fingers) */.onEnded{valuein/* do something */}}
.updating
is better in most of the cases because you care only the relative change
Demo (22:21)
UserDefaults.standard.set(emojiArt.json, forKey: "EmojiArtDocument.Untitled")
Codable
property means that struct can be encoded and decoded
varjson:Data?{returntry?JSONEncoder().encode(self)}init?(json:Data?){ifjson!=nil,letnewEmojiArt=try?JSONDecoder().decode(EmojiArt.self,from:json!){self=newEmojiArt}else{returnnil}}
Double tap makes zooms in a way that you can see the full image
....gesture(self.doubleTapToZoom(in:geometry.size))...privatefuncdoubleTapToZoom(insize:CGSize)->someGesture{TapGesture(count:2).onEnded{withAnimation{self.zoomToFit(self.document.backgroundImage,in:size)}}}privatefunczoomToFit(_image:UIImage?,insize:CGSize){ifletimage=image,image.size.width>0,image.size.height>0{lethZoom=size.width/image.size.widthletvZoom=size.height/image.size.heightself.zoomScale=min(hZoom,vZoom)}}
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse