Post

Replies

Boosts

Views

Activity

Reply to Semaphore
Class c issues semaphore wait That's a super-bad practice in programming iOS or macOS (or any other GUI platforms that main thread may have special meaning). You need to find another way than using semaphore.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Swift for server-side
There's no server side frameworks for Swift by Apple. And you would not be able to find any frameworks as widely used as .NET as of now and in the near future. I'm quite critical if Apple would support server-side Swift in the future. You should better visit swift.org (especially Swift on Server) and all related articles on the web.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Semaphore
Are you saying using semaphores to wait until download is complete a bad practice? Sure.  If so, can you suggest a good practice? Use completion handler properly, or async/await if you can target new OSs coming this fall.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Semaphore
Ok, what is the best practice other than async/wait as I have to implement before this fall. Already written: Use completion handler properly Something like this: class A { func aMethod() { B().download(downloadedData: { data in guard let data = data else { return } //Use `data` here... }) } } class B { func download(downloadedData: @escaping (_ data: Data? ) -> Void ) { NetworkManager().downloadRequest {result in switch result { case .success(let data): //..... downloadedData(data) case .failure(let error): print(error) //... downloadedData(nil) } } } } There's one simple principle: Never try to use asynchronous methods synchronously. You may want to use semaphores when you have multiple background threads and make them work together with some limited resources. But never for just waiting an asynchronous task to finish.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to I'm trying to understand Combine Future
Please try something like this: import Foundation import Combine import SwiftUI let future = Future<Int, Never> { promise in print("Creating") DispatchQueue.global().asyncAfter(deadline: .now() + 2) { print("sending promise") promise(.success(1)) } } let cancellable = future //<- .sink(receiveCompletion: { print("receiveCompletion:\($0)") }, receiveValue: { print("receiveValue:\($0)") }) print("end") It seems the result of future.sink needs to be held in a strong reference until the operation is completed.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Cannot use instance member 'retreiveFromArrayOfMeme' within property initializer
the following code in my class MemeMeViewModel which is an ObservableObject  You should better include the class header when you show some parts of a class. I have the following error : "Cannot use instance member 'retreiveFromArrayOfMeme' within property initializer; property initializers run before 'self' is available". As the error message is clearly stating, you cannot use the instance method retreiveFromArrayOfMeme within property initializer. One possible fix would be making it a static method in this case: @Published var memes: [MemeMeModel.Meme] = MemeMeViewModel.retreiveFromArrayOfMeme() static func retreiveFromArrayOfMeme() -> [MemeMeModel.Meme] { var temp = [MemeMeModel.Meme]() if let memes = UserDefaults.standard.object(forKey: "MemesList") as? [[Any]] { for meme in memes { temp.append(MemeMeModel.Meme(pngRepresentation: meme[0] as! Data, content: meme[1] as! String)) } } return temp } (In Swift, only type names start with Capital letter.) You may need to find other ways when you want use some other instance members in retreiveFromArrayOfMeme().
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’21
Reply to Checking for characters
When you show your code, please use the Code Block feature of this site: (And a good indentation would make the code far more readable, Ctrl-I in Xcode editor will do it for you.) let tenMostCommonPasswords = [ "123456", "password", "12345678", "qwerty", "12345", "123456789", "letmein", "1234567", "football", "iloveyou" ] let digits = "0123456789" let punctuation = #"!@#$%^&*(),.<>;'`~[]{}\|/?_-+= "# let password = "Pasword12!" for _ in tenMostCommonPasswords { if tenMostCommonPasswords.contains(password) { print("Please change your password") } else { for _ in digits { if digits.contains(password) { for _ in punctuation { if punctuation.contains(password) { print("You're good to go!") } else { print("Please add a punctuation") } } } else { print("Please add a digit.") } } } } The expression digits.contains(password) gets true when digits("0123456789") contains the string password ("Pasword12!"). But as you see, "0123456789" does not contain "Pasword12!". So the else-close will be executed. I guess you may want to do something like this: if tenMostCommonPasswords.contains(password) { print("Please change your password") } else { for digitChar in digits { if password.contains(digitChar) { for punctuationChar in punctuation { if password.contains(punctuationChar) { print("You're good to go!") } else { print("Please add a punctuation") } } } else { print("Please add a digit.") } } } You may find this code needs to be fixed a little more, but it might be a good habit to leave a room for learning by yourself.
Topic: Business & Education SubTopic: General Tags:
Aug ’21
Reply to Current Xcode/SDK requirements?
You should better check the announcements of Apple by yourself: News and Updates As the time of this submission, this seems to be the latest: App Store submission update Starting April 26, 2021, all iPhone and iPad apps submitted to the App Store must be built with Xcode 12 and the iOS 14 SDK or later. Starting April 26, 2021, all watchOS apps submitted to the App Store must be built with Xcode 12 and the watchOS 7 SDK or later. You can still use Xcode 11 for some other purposes, such as learning coding, but you need to use Xcode 12 (or later) if you are planning to write an app for App Store.
Aug ’21
Reply to Segmentation fault vs Variable ... used before being initialized
Is this a compiler bug in the newest version? why would we trade a compiler error for a runtime crash Isn't it clear it is a compiler bug? According to the accepted proposal of propertyWrapper (SE-0258Property Wrappers), declaring an @State variable is something similar to this: private var _player: State<AVPlayer> = State<AVPlayer>(wrappedValue: 1738) var player: AVPlayer { get { return _player.wrappedValue } set { _player.wrappedValue = newValue } } (Simplified to illustrate the problem.) As you see, the property player is a computed property, so, based on the initialization rule of Swift, it will be available only after all the stored properties are initialized. Swift compiler should mark the line self.player = AVPlayer() error. You can send a bug report to Apple or swift.org. By the way, creating a useless instance like AVPlayer() is not recommended. You should use Optional if you cannot use AVPlayer.init(url:) or AVPlayer.init(playerItem:) at the time of initialization.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Actors and async/await testing before macOS 12?
Have you tried creating an iOS App project, or an iOS Playground? You may not be able to test some macOS-only features, but it may be enough for testing simple codes using actors and async/await.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Cannot use instance member 'dateType' within property initializer; property initializers run before 'self' is available
As the error message is clearly stating, you cannot use other instance properties inside an initializer of an instance property. One possible solution would be initializing the property dailyVax inside onAppear of your View.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Will casting "as?" fail if not the correct type?
Unless, theNode is of type MyPeg or is of any subclasses of MyPeg, theNode as? MyPeg will return nil and the if-statement will fail. No same size, being similar nor having the same variables do not effect.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Semaphore
Class c issues semaphore wait That's a super-bad practice in programming iOS or macOS (or any other GUI platforms that main thread may have special meaning). You need to find another way than using semaphore.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Swift for server-side
There's no server side frameworks for Swift by Apple. And you would not be able to find any frameworks as widely used as .NET as of now and in the near future. I'm quite critical if Apple would support server-side Swift in the future. You should better visit swift.org (especially Swift on Server) and all related articles on the web.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Semaphore
Are you saying using semaphores to wait until download is complete a bad practice? Sure.  If so, can you suggest a good practice? Use completion handler properly, or async/await if you can target new OSs coming this fall.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Semaphore
Ok, what is the best practice other than async/wait as I have to implement before this fall. Already written: Use completion handler properly Something like this: class A { func aMethod() { B().download(downloadedData: { data in guard let data = data else { return } //Use `data` here... }) } } class B { func download(downloadedData: @escaping (_ data: Data? ) -> Void ) { NetworkManager().downloadRequest {result in switch result { case .success(let data): //..... downloadedData(data) case .failure(let error): print(error) //... downloadedData(nil) } } } } There's one simple principle: Never try to use asynchronous methods synchronously. You may want to use semaphores when you have multiple background threads and make them work together with some limited resources. But never for just waiting an asynchronous task to finish.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to ForEach error with array of videos
With Xcode 13 beta 3, your code causes Missing argument for parameter 'id' in call on the line ReelsPlayer(reel: $reel). Which version of Xcode are you using?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to I'm trying to understand Combine Future
Please try something like this: import Foundation import Combine import SwiftUI let future = Future<Int, Never> { promise in print("Creating") DispatchQueue.global().asyncAfter(deadline: .now() + 2) { print("sending promise") promise(.success(1)) } } let cancellable = future //<- .sink(receiveCompletion: { print("receiveCompletion:\($0)") }, receiveValue: { print("receiveValue:\($0)") }) print("end") It seems the result of future.sink needs to be held in a strong reference until the operation is completed.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Cannot use instance member 'retreiveFromArrayOfMeme' within property initializer
the following code in my class MemeMeViewModel which is an ObservableObject  You should better include the class header when you show some parts of a class. I have the following error : "Cannot use instance member 'retreiveFromArrayOfMeme' within property initializer; property initializers run before 'self' is available". As the error message is clearly stating, you cannot use the instance method retreiveFromArrayOfMeme within property initializer. One possible fix would be making it a static method in this case: @Published var memes: [MemeMeModel.Meme] = MemeMeViewModel.retreiveFromArrayOfMeme() static func retreiveFromArrayOfMeme() -> [MemeMeModel.Meme] { var temp = [MemeMeModel.Meme]() if let memes = UserDefaults.standard.object(forKey: "MemesList") as? [[Any]] { for meme in memes { temp.append(MemeMeModel.Meme(pngRepresentation: meme[0] as! Data, content: meme[1] as! String)) } } return temp } (In Swift, only type names start with Capital letter.) You may need to find other ways when you want use some other instance members in retreiveFromArrayOfMeme().
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Checking for characters
When you show your code, please use the Code Block feature of this site: (And a good indentation would make the code far more readable, Ctrl-I in Xcode editor will do it for you.) let tenMostCommonPasswords = [ "123456", "password", "12345678", "qwerty", "12345", "123456789", "letmein", "1234567", "football", "iloveyou" ] let digits = "0123456789" let punctuation = #"!@#$%^&*(),.<>;'`~[]{}\|/?_-+= "# let password = "Pasword12!" for _ in tenMostCommonPasswords { if tenMostCommonPasswords.contains(password) { print("Please change your password") } else { for _ in digits { if digits.contains(password) { for _ in punctuation { if punctuation.contains(password) { print("You're good to go!") } else { print("Please add a punctuation") } } } else { print("Please add a digit.") } } } } The expression digits.contains(password) gets true when digits("0123456789") contains the string password ("Pasword12!"). But as you see, "0123456789" does not contain "Pasword12!". So the else-close will be executed. I guess you may want to do something like this: if tenMostCommonPasswords.contains(password) { print("Please change your password") } else { for digitChar in digits { if password.contains(digitChar) { for punctuationChar in punctuation { if password.contains(punctuationChar) { print("You're good to go!") } else { print("Please add a punctuation") } } } else { print("Please add a digit.") } } } You may find this code needs to be fixed a little more, but it might be a good habit to leave a room for learning by yourself.
Topic: Business & Education SubTopic: General Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to Current Xcode/SDK requirements?
You should better check the announcements of Apple by yourself: News and Updates As the time of this submission, this seems to be the latest: App Store submission update Starting April 26, 2021, all iPhone and iPad apps submitted to the App Store must be built with Xcode 12 and the iOS 14 SDK or later. Starting April 26, 2021, all watchOS apps submitted to the App Store must be built with Xcode 12 and the watchOS 7 SDK or later. You can still use Xcode 11 for some other purposes, such as learning coding, but you need to use Xcode 12 (or later) if you are planning to write an app for App Store.
Replies
Boosts
Views
Activity
Aug ’21
Reply to Started to learn Swift and got stuck...
In my Xcode 12.5.1 (Big Sur 11.4), it works perfectly as expected. Do you think of anything special for your environment? Have you checked you can successfully build your project before trying Refactor > Extract to Method?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to Segmentation fault vs Variable ... used before being initialized
Is this a compiler bug in the newest version? why would we trade a compiler error for a runtime crash Isn't it clear it is a compiler bug? According to the accepted proposal of propertyWrapper (SE-0258Property Wrappers), declaring an @State variable is something similar to this: private var _player: State<AVPlayer> = State<AVPlayer>(wrappedValue: 1738) var player: AVPlayer { get { return _player.wrappedValue } set { _player.wrappedValue = newValue } } (Simplified to illustrate the problem.) As you see, the property player is a computed property, so, based on the initialization rule of Swift, it will be available only after all the stored properties are initialized. Swift compiler should mark the line self.player = AVPlayer() error. You can send a bug report to Apple or swift.org. By the way, creating a useless instance like AVPlayer() is not recommended. You should use Optional if you cannot use AVPlayer.init(url:) or AVPlayer.init(playerItem:) at the time of initialization.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to On first click the share sheet shows nothing
You should better show the whole code of the core files causing the issue. Or at least you should show in which file each code block of yours exists. Not all readers have enough time to explore files in a GitHub repository.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’21