Explore tweets tagged as #100DaysOfPROSwift
Day 31 of #100DaysOfPROSwift. π Donβt use UUID().uuidString to generate identifiers unless you truly need a string. Instead of this:. π« let id = UUID().uuidString.π¬ You lose type safety and introduce fragile string-based comparisons. Do this:. β
let id = UUID().Swiftβs UUID
0
0
0
Day 26 of #100DaysOfPROSwift. ποΈ Observe property changes with KVO β clean, automatic, and no delegates needed. Instead of this:. π« Manually updating state or using custom delegate.π¬ More boilerplate, easy to forget updates. Do this:. β
Use @objc dynamic + observe to track
0
0
0
Day 34 of #100DaysOfPROSwift. π Use enum with associated values to model complex state β cleanly and safely. Instead of this:. var isLoading = false .var errorMessage: String? .var data: Data? . Scattered state, easy to mismanage and misrepresent. Do this:. enum LoadState {
0
0
0
Day 37 of #100DaysOfPROSwift. π Use zip(_:_:) to iterate over two collections in parallel β cleanly and safely. Instead of this:. for i in 0. <min(names.count, ages.count) { . print("\(names[i]) is \(ages[i]) years old") .} . π¬ Manual index math is error-prone and ugly. Do
0
0
0
Day 35 of #100DaysOfPROSwift. π Use typealias to simplify complex types and improve readability. Instead of this:. let handler: (Result<Data, NetworkError>) -> Void . Hard to read, especially when reused in multiple places. Do this:. typealias DataResultHandler = (Result<Data,
0
0
0
Day 32 of #100DaysOfPROSwift. π Prefer using .contains(_:) over manual loops to check for values in collections. Instead of this: .for item in array { . if item == target { . found = true . } .} .π¬ Verbose and error-prone β too much boilerplate. Do this:.β
0
0
0
Day 28 of #100DaysOfPROSwift. π Use swapAt(_:_:) to swap elements in an array β safely and concisely. Instead of this:. π« Manual temp variables.π¬ Verbose and error-prone. Do this:. β
array.swapAt(i, j). Swift gives you a built-in method to swap elements directly by index.
0
0
2
Day 39 of #100DaysOfPROSwift. π Use @autoclosure to delay evaluation of expressions β without changing your function call syntax. Instead of this:. func log(_ message: () -> String) { . if debugMode { print(message()) } .} . log({ "Heavy log: \(expensiveCalculation())" })
0
0
0
Day 38 of #100DaysOfPROSwift. π Use trailing closure syntax to keep function calls clean and expressive. Instead of this:. let result = { name in . name.uppercased() .}). Cluttered with unnecessary parentheses and syntax. Do this:. let result =
0
0
0
Day 27 of #100DaysOfPROSwift. π‘ Safely access array elements with a custom subscript. Developers often do this:. π« let item = array[3].π¬ Risky β crashes if the index is out of bounds. Do this instead:. extension Array {. subscript(safe index: Int) -> Element? {
0
0
0
Day 40 of #100DaysOfPROSwift. Use guard case let to elegantly unwrap and pattern-match tuples. Instead of this:. if let status = response.status,. let data = . status == .success {. handle(data).} else {. return.}. π¬ Nested if chains get messy
0
0
0
Day 29 of #100DaysOfPROSwift. π Use Swiftβs KeyPath to dynamically access or sort properties β without hardcoding field names. Instead of this:.π« users.sorted { $0.name < $1.name }.π¬ Repetitive and fragile in generic contexts. Do this:.β
users.sorted(by: \.name). KeyPaths
0
0
0
Day 36 of #100DaysOfPROSwift. π Use Dictionary(grouping:by:) to organize items by category β in one line. Instead of this:. var grouped: [String: [User]] = [:] .for user in users { . grouped[user.role, default: []].append(user) .} .π¬ Verbose and easy to mess up. Do this:
0
0
0
Day 33 of #100DaysOfPROSwift. π Use CaseIterable to access all cases of an enum β no manual lists needed. Instead of this:. let allOptions = [.optionA, .optionB, .optionC]. Manual case lists break easily if the enum changes. Do this:. enum Option: CaseIterable {. case a, b, c
0
0
0
Day 30 of #100DaysOfPROSwift. π Use first(where:) to find the first element in a collection that matches a condition β efficiently and elegantly. Instead of this:. π« Manual loops to check each element.π¬ Risk of out-of-bounds errors or verbose code. Do this:. β
0
0
0
Day 44 of #100DaysOfPROSwift. Use String(repeating:count:) to quickly generate patterns π. Instead of this:.π¬.var dashes = "".for _ in 0. <10 {. dashes += "-".}. Do this:. let dashes = String(repeating: "-", count: 10). β
Shorter, clearer, and more efficient. Swiftβs.
0
0
2
Day 43 of #100DaysOfPROSwift. Use Swiftβs new native Regex for powerful pattern matching π§ .Instead of this: . let range = text.range(of: "[a-z]+", options: .regularExpression). Do this:. if let match = try? /[a-z]+/.firstMatch(in: text) {. // use match.}. β
Native syntax,.
0
0
4
Day 41 of #100DaysOfPROSwift. Use switch with tuples to match multiple conditions at once. Instead of this:. if user.isLoggedIn && user.hasPremium {. showDashboard().} else if user.isLoggedIn {. showUpgradeScreen().} else {. showLogin().}. π¬ Too many chained if conditions.
0
0
2
Day 42 of #100DaysOfPROSwift. Build strings the Swifty way with interpolation π§©. Instead of this: π¬. let message = "Hello, " + name + "! You have " + String(count) + " new messages.". Do this:. let message = "Hello, \(name)! You have \(count) new messages.". β
Safer, cleaner,.
0
0
0
Day 45 of #100DaysOfPROSwift. Super-charge your data processing with concurrent mapping via TaskGroup β‘οΈ. Instead of this:. π¬ Traditional loop, one network call at a time. var images: [UIImage] = [].for url in urls {. if let img = try? await fetchImage(url) {.
0
0
4