#swifttip search results
๐ก #SwiftTip: Multiple SwiftUI Preview Providers Did you know that you can have any number of #SwiftUI preview providers in a single file? Xcode will group them, pretty print their names, and add a separator.
Use this code tip if you want trigger some functionality if you get back in your VC AND not the first time you present it ๐๐ป #swifttip #iosdev
๐ก #SwiftTip: KeyPath-Based Builder ๐ทโโ๏ธ Here is a cool trick with #Swift KeyPaths โ you can implement a universal Builder pattern once and reuse it everywhere. Great to make your APIs more expressive and readable.
#SwiftTip You can gracefully avoid nested do/catch with a simple extension on the Result type. Really useful if you want to try different things before eventually fallback on a default value. gist.github.com/jegnux/500b097โฆ
๐ก #SwiftTip: Calling `willSet` and `didSet` from initializer By default, `willSet` and `didSet` observers are not called when a property is first initialized. We can overcome this by using the `defer` statement inside the `init` method ๐
๐ฏ Swift tip #7: Comparing boolean conditions with switch instead of if-elseif-elseif-else. Additionally compiler makes sure that all the possible cases are handled. #swifttip #xcode #iosdev
๐ก #SwiftTip: Avoiding Degenerate Optional Values Avoid designing your code with `Optional` collections and booleans. Otherwise, you will run into a degenerate case, where it is unclear what does "nothing" mean. Is it an empty array or `nil` array; is it `false` or `nil`?
#SwiftTip I know that there is a bunch of uses for SwiftUI previews, but I just found out that it can be used for convenient DateFormatter and NumberFormatter configuration
๐ก#SwiftTip: Result<T, Never> Set Result.Failure type to Never in case error is impossible or can be replaced with a value. E.g., pass an empty array if an array of elements has failed to load. The compiler is smart and does not require us to handle the failure case in switch.
๐ก #SwiftTip: 'Y' vs 'y' formatter ๐ โข 'y' specifies the calendar year. โข 'Y' specifies the ISO8601 year-week calendar. โข Always use 'y' when presenting dates to users. ๐ ISO8601 counts full weeks. In 2021 Jan 1st falls on Friday, thus the mismatch is Dec 29 - 31.
๐ก #SwiftTip: Configuring UIAlertControllers Here is a neat way of configuring alert controllers โ you can declare a factory method for each style. It cuts boilerplate code, organizes your alerts in one place and makes them more readable.
๐ก #SwiftTip Debug Log Optionals without Warnings Remember this problem when you debug log or display an optional value on UI, it prints 'Optional(...)' and gives a warning? The solution is to provide a custom string interpolation for Optional<T>. See the attached screenshot.
#SwiftTip Una de las curiosidades de usar operadores Bit a Bit que se puede usar para setear una variable a 0 de la forma mรกs eficiente por la CPU "Bueno eso en ASM si lo es, XD)
๐ฏ Swift tip #8: Dictionary has a subscript with default value. Useful if accessing the dictionary should return a fallback value when the dictionary key is not present. #swifttip #xcode #iosdev
๐ก #SwiftTip: KeyPath Builder ๐ทโโ๏ธ Here is a cool trick with Swift KeyPaths โ you can implement a universal Builder pattern once and reuse it everywhere. Great to make your APIs more expressive and readable.
๐ฏ Optional pattern matching in for loop only loops over non-nil values #swiftlang #swifttip #iosdev
Sad that you're still stuck on Mohave and you can't use the fancy new Previews ๐? You can run your SwiftUI Views in an Xcode 11 playground by wrapping it in a UIHostingController. ๐ #swifttip #swiftuitip
โฑ๏ธSwift 5.7 in Xcode 14 has a new ContinuousClock API which also provides a nice way to measure time of async functions. #swiftlang #swifttip #iosdev
#SwiftTip: Use the reduce() method to combine all elements of an array into a single value. ```swift let numbers = [1, 2, 3, 4, 5] let sum = numbers.reduce(0, +) // 15 ``` #Swift
#Swift Tip ๐ก #SwiftTip ๐ก Initialize an array with a specified size using the `init(repeating:count:)` initializer. Example: ```swift let array = Array(repeating: "Swift", count: 3) // ["Swift", "Swift", "Swift"] ```
#Swift tip: Use the `reduce` method to iterate over an array and apply a transformation to each element. Here's an example: let numbers = [1, 2, 3, 4, 5] let sum = numbers.reduce(0, +) // Result: 15 #SwiftTip #ArrayManipulation #FunctionalProgramming
**Swift** #swiftTip: Use `reduce` to iterate over a collection and accumulate a single value. ```swift let numbers = [1, 2, 3, 4, 5] let sum = numbers.reduce(0, +) print(sum) // 15 ``` #Swift #programming #dev
Swift tip โก๏ธ: For a quick debug print, use `print(thing, terminator: "")` to print to the same line instead of a new line. You can follow it up with `print()` to return to a new line. #swift #swiftlang #swifttip
#SwiftTip: Use the ?? operator to unwrap an optional value safely. If the value is nil, the default value will be used. Example: ```swift let name: String? = nil let displayName = name ?? "Unknown" // displayName will be "Unknown" ```
Foundation's partition(by:) is useful if we want to divide a collection into two parts and operate on both sub-collections. Example could be partitioning index paths and calling UITableView reloadRows for one part and reconfigureRows for the other part. #swifttip #iosdev
When dealing with shared instances, sometimes it makes sense to introduce static subscript for skipping typing .shared everywhere. #swifttip #iosdev
Using enums over structs for namespacing static constants. Nicer since we can't create an instance of AppConstants nor AppConstants.URLs. More Swift tips: bit.ly/swifttips #swiftlang #swifttip
Destructuring a tuple is a short way to extract values from tuples in Swift #swifttip #swiftlang #iosdev
In addition to functions and properties, we can add init methods to protocols in Swift. In that case the comforming non-final class needs use the โrequiredโ modifier. This ensures that the init is explicitly implemented by the class or its subclasses. #swifttip #swiftlang #iosdev
Reduce boilerplate and harness the power of protocol-oriented programming in #Swift! protocol Identifiable { var id: Int { get set } } Now each of your models can conform and be 'identifiable'! #SwiftTip ๐ค
Swift 5.9 added sleep(for:) to the Clock protocol rather than having to define an instant. github.com/apple/swift-evโฆ #swifttip #swiftlang #iosdev
Combat #carpetstains like a pro with this #swifttip! ๐งผ When spills happen, act fast. Gently blot the area using a clean cloth or paper towel. Begin from the outer edges and work your way toward the center to avoid #spreading the stain. Quick action, lasting cleanliness! โจ๐๏ธ
If a function in Swift has multiple defer statements then these run in a reverse order. Something to keep in mind if the order is happens to be important. #swifttip #swiftlang #iosdev
In Swift 5.8 we can opt-in to a new #file behavior where it equals to the module_name/file_name instead of the full path. Useful when implementing custom logging functions. #swifttip #iosdev #swiftlang
Swift 5.8 in Xcode 14.3 implements @backDeployed attribute for adding functionality to older API versions! ๐ #swifttip #iosdev gist.github.com/laevandus/af00โฆ
When using switch for enums with associated values and we do not want to use associated values then we can only write out the case name .error (instead of `.error(_)`) gist.github.com/laevandus/dd1dโฆ #swifttip #iosdev
Adding custom init in an extension keeps the auto-generated init for structs in Swift gist.github.com/laevandus/d541โฆ #swifttip #iosdev
๐ก #SwiftTip: Multiple SwiftUI Preview Providers Did you know that you can have any number of #SwiftUI preview providers in a single file? Xcode will group them, pretty print their names, and add a separator.
๐ก #SwiftTip: KeyPath-Based Builder ๐ทโโ๏ธ Here is a cool trick with #Swift KeyPaths โ you can implement a universal Builder pattern once and reuse it everywhere. Great to make your APIs more expressive and readable.
๐ฏ Swift tip #4: Use dump() for printing out all the properties of a type. Especially useful when the type has a custom description which reveals only some information. #swifttip #swiftlang #xcode #iosdev
Use this code tip if you want trigger some functionality if you get back in your VC AND not the first time you present it ๐๐ป #swifttip #iosdev
๐ฏ Swift tip #7: Comparing boolean conditions with switch instead of if-elseif-elseif-else. Additionally compiler makes sure that all the possible cases are handled. #swifttip #xcode #iosdev
๐ฏ Swift/iOS/macOS tip #5: HTTPURLResponse has a function for returning localised strings for HTTP status codes. #swifttip #xcode #iosdev #macosengineer
๐ก #SwiftTip: Calling `willSet` and `didSet` from initializer By default, `willSet` and `didSet` observers are not called when a property is first initialized. We can overcome this by using the `defer` statement inside the `init` method ๐
๐ฏ Swift tip #1: Avoid optional when converting data to utf8 string with init(decoding:as:) #swiftlang #swifttip #iosengineer #iosdev
๐ก#SwiftTip: Result<T, Never> Set Result.Failure type to Never in case error is impossible or can be replaced with a value. E.g., pass an empty array if an array of elements has failed to load. The compiler is smart and does not require us to handle the failure case in switch.
๐ก #SwiftTip: Configuring UIAlertControllers Here is a neat way of configuring alert controllers โ you can declare a factory method for each style. It cuts boilerplate code, organizes your alerts in one place and makes them more readable.
๐ก #SwiftTip: 'Y' vs 'y' formatter ๐ โข 'y' specifies the calendar year. โข 'Y' specifies the ISO8601 year-week calendar. โข Always use 'y' when presenting dates to users. ๐ ISO8601 counts full weeks. In 2021 Jan 1st falls on Friday, thus the mismatch is Dec 29 - 31.
๐ก #SwiftTip: Avoiding Degenerate Optional Values Avoid designing your code with `Optional` collections and booleans. Otherwise, you will run into a degenerate case, where it is unclear what does "nothing" mean. Is it an empty array or `nil` array; is it `false` or `nil`?
๐จ Xcode tip #23: Sort files and folders by name by right clicking on a folder and selecting "Sort By Name" #xcodetip #swifttip #iosdev
๐ก #SwiftTip Debug Log Optionals without Warnings Remember this problem when you debug log or display an optional value on UI, it prints 'Optional(...)' and gives a warning? The solution is to provide a custom string interpolation for Optional<T>. See the attached screenshot.
๐ฃ Swift Tip ๐ฃ Controla el acceso a tu cรณdigo. Usa siempre el modificador de acceso apropiado para encapsular tu cรณdigo. En Swift tienes 3 modificadores: private, internal y public. #SwiftTip #iOS #MiraMiCodigo
๐ฏ Swift tip #8: Dictionary has a subscript with default value. Useful if accessing the dictionary should return a fallback value when the dictionary key is not present. #swifttip #xcode #iosdev
โฑ๏ธSwift 5.7 in Xcode 14 has a new ContinuousClock API which also provides a nice way to measure time of async functions. #swiftlang #swifttip #iosdev
๐ฏ Optional pattern matching in for loop only loops over non-nil values #swiftlang #swifttip #iosdev
๐ฏ Swift tip #2: Converting an integer to data and back. Useful when working with raw bytes. #swiftlang #swifttip #xcode #iosengineer #macosengineer #iosdev
Something went wrong.
Something went wrong.
United States Trends
- 1. #DWTS 44.1K posts
- 2. Northern Lights 19.5K posts
- 3. Justin Edwards 1,266 posts
- 4. #Aurora 4,113 posts
- 5. Andy 59.4K posts
- 6. Louisville 12.7K posts
- 7. Elaine 46.2K posts
- 8. #RHOSLC 5,224 posts
- 9. Robert 98.8K posts
- 10. #WWENXT 14.3K posts
- 11. Kentucky 22.6K posts
- 12. Dylan 30.7K posts
- 13. Lowe 11.7K posts
- 14. Celtics 10K posts
- 15. Whitney 8,646 posts
- 16. Oweh 1,430 posts
- 17. Jordan Walsh N/A
- 18. Alix 9,059 posts
- 19. Kam Williams N/A
- 20. #OlandriaxHarpersBazaar 1,018 posts