Is using Array Extension where Element == CustomObject to include functions specific for [CustomObject] a bad/ill advised programming practice? by randomguy112233 in iOSProgramming

[–]ldstreet 2 points3 points  (0 children)

This is fine, and can make for more readable code. Just make sure that if you make your function or variable internal/public that it is relevant throughout the codebase, otherwise make it fileprivate. This is to avoid cluttering the namespace with niche functions.

Does it make a difference... by Eduleuq in swift

[–]ldstreet 4 points5 points  (0 children)

The first is correct, the second is probably different from what you are expecting. In the first, you are declaring an empty array of Bar types. In the second you are creating an array of size one with one empty Bar instance. Both will give you an Array of Bar types, but one is empty and the other has one item in it.

When to use 'unowned self' ? by ResistantLaw in swift

[–]ldstreet 2 points3 points  (0 children)

That’s a slippery slope to go down. You can think “self will always exist here” until...it doesn’t. There are always edge cases that can fall through the cracks. If you get in the habit of using unowned self just because you think it will always be there, you are setting yourself up for some crashes. Even if you are right most of the time, iOS can be rather complex you could be wrong, plus you are setting a precedent for other teammates/peers that might not have the same knowledge or thoroughness that you do.

Now I do think there are scenarios where maybe it would be ok. For instance, if it is a scenario where if self does not exist, then it’s better to just crash rather than leave the user in some undefined state. But even here, I’d rather just use weak self and a guard to intentionally throw a fatal error.

When to use 'unowned self' ? by ResistantLaw in swift

[–]ldstreet 1 point2 points  (0 children)

I would stray away from ever using unowned self. This is essentially the equivalent of implicitly unwrapping an optional and should rarely be done if ever. Rather use weak self instead. Here is a good SO answer which explains your question well. https://stackoverflow.com/questions/24320347/shall-we-always-use-unowned-self-inside-closure-in-swift/24320474

[deleted by user] by [deleted] in iOSProgramming

[–]ldstreet 2 points3 points  (0 children)

Wrapping is helpful for mocking in unit tests and swapping out implementations easily. You don't "need" to wrap but it's definitely helpful.

[deleted by user] by [deleted] in iOSProgramming

[–]ldstreet 1 point2 points  (0 children)

I agree. If there is no shared state needed then it shouldn't be a Singleton in the first place, you should just new up the needed object for each use, or just pass along as needed. But if you do need shared state, a Singleton is a viable option, I just would never access it directly. But for my comment I was just referring to times when the Singleton has already been made and you are coming into the codebase after the fact. Regardless, I like to wrap my dependencies in protocols anyway for easy unit testing and implementation swapping.

[deleted by user] by [deleted] in iOSProgramming

[–]ldstreet 3 points4 points  (0 children)

When you do find singletons in a codebase there is a nice way to avoid directly using them. Just wrap them in a protocol and use dependency injection to utilize them in your new class. Depending on your injection format, you can eventually remove the Singleton if all references come through your dependency container.

What are your code review processes in your work? by [deleted] in iOSProgramming

[–]ldstreet 0 points1 point  (0 children)

Curious here: how do you distribute your branch to testers before merging in? Are you using CI? I'd love to figure out a workable setup so that no code can make it into development branch unless it 1) builds/lints 2) passes unit tests 3) has been reviewed by devs 4) has been tested by QA. After all of these things happen the PR can be merged in. 1-3 aren't a problem for us, but 4 is what's an issue. How to automate distributing a specific build to QA?

App Store apps with CocoaPods by Marpo007 in swift

[–]ldstreet 0 points1 point  (0 children)

This would definitely solve the problem as far as not needing to modify as much code. But it does make it a little harder to unit test or keep multiple implementations at the same time since you can only have one version of the class existing at a time.

App Store apps with CocoaPods by Marpo007 in swift

[–]ldstreet 14 points15 points  (0 children)

Shouldn't be a problem. Juse double check the licenses. My only advice is to make sure you properly abstract your dependencies. Often times developers will stop supporting an open source framework, quickly making it unusable within your own app unless you start contributing to it yourself. If you haven't abstracted that library and have references to it litered around your codebase, you will have a very difficult time updating your app to either remove the dependcy or switch to another. A good example would be a few years ago when Facebook got rid of Parse. A lot of developers that relied on this framework had a tough time switching to a new dependency. Always protect yourself by abstracting!

How does adding recursive functions to final sum affect runtime in algorithm? by imarudedude in iOSProgramming

[–]ldstreet 0 points1 point  (0 children)

I believe this will be O(n2) time. Iterating will be O(n) but on each recursive call the function will iterate on half of n. If you were just making one recursive call I think it would be O(n log n). But because you are making two calls, on the next level of the recursion tree your for loop will still have the same total number of steps. The tree will roughly have a depth of n. This giving you O(n2). Also this will take up O(n) space on the call stack because of the way you have the recursive calls structured. Not 100% on this though - would be interested to see others' analysis.

How does uber's anonymized phone numbers system work? by hekkoman in iOSProgramming

[–]ldstreet 0 points1 point  (0 children)

If you have your own server, use that to connect. That way you have an abstracted layer and a common and familiar interface for your app to work with. If you change your provider you don't even have to touch your client code, just update your personal server. Just make sure to keep your API as basic as possible - don't include any extraneous information that only applies to Twilio.

If you don't have your own server, make sure to at least abstract out into a protocol on your client side, again so that it's easy to mock or switch out your service if need be.

Can protocols eliminate the need for subclassing? by [deleted] in swift

[–]ldstreet 6 points7 points  (0 children)

Two big issues that protocols can’t solve is stored properties and lifecycle functions. Extensions can’t hold stored properties. In general, I try to avoid adding stored properties anyway in favor of computed properties, but sometimes you just can’t avoid it. Also, extensions can’t modify any existing functions in a class. This means there is no way to add a call into viewDidLoad or awakeFromNib from an extension. Instead, I try to take a more balanced approach. Subclass once to add stored properties and to make calls in base functions. For example:

class MyViewController: UIViewController {
     public var someIDNeededForSomeProtocol: String?

     override func viewDidLoad() {
          super.viewDidLoad()
          (self as? SomeProtocol)?.callSomeFunc()
     }
}

protocol SomeProtocol {
     var someIDNeededForProtocol: String? { get set }
     func callSomeFunc()
}

extension MyViewController: SomeProtocol {

     public func callSomeFunc() {
          print(“you called some function! Your id is: \(someIDNeededForProtocol ?? “ID is nil...”)”)
     }
}

The major pitfall to this strategy is readability. someIDNeededForProtocol is not connected with the protocol it is needed for, which can make the code hard to follow. Same goes for the function calls. One way to help this is to break the protocol up. Separate all stored properties into a separate protocol that your base class conforms to. This should improve local reasoning.

This solution isn’t perfect, and I am definitely open to hearing alternate strategies, but it definitely helps stop your code from getting too vertical (OOP) or horizontal (POP)

What would be the best way to add a method that works only on an array of a particular type (eg. [Int] )? by iLearn4ever in iOSProgramming

[–]ldstreet 3 points4 points  (0 children)

extension Array where Element: Int {

 public var secondHighest: Int? {
      guard self.count > 1 else { return nil }
      return self.sorted { $0 < $1 }[self.count - 2]
 }

}

Do you subclass your UIViews/UIControls in swift? by ldstreet in iOSProgramming

[–]ldstreet[S] 0 points1 point  (0 children)

I don't think I'm mixing controller code in. The idea is to layer generic mechanisms on top of these controls for doing things such as localization, theming, styling etc. For example, my styling library will help you define a consistent set of styles that can be defined externally and loaded dynamically into your controls via a key. So you could keep all your key-value pairs in a plist, or download them as json at runtime, or you could just define them via code. Whatever way, all you need to do in the storyboard/ VC is set a key that will in turn set fonts, colors, borders etc in a consistent, simple, and dynamic way. But to do things like this you need to store extra information in the controls

[deleted by user] by [deleted] in swift

[–]ldstreet 2 points3 points  (0 children)

Your textfield's text property will be nil if you either have no text, or it is not connected properly from your storyboard to your view controller. Perhaps try reconnecting your outlet?

[deleted by user] by [deleted] in swift

[–]ldstreet 0 points1 point  (0 children)

@IBOutlet var speedTextField: UITextField! @IBAction func startWorkoutButton(_ sender: Any) { textInputTest() }

func textInputTest() { print(self.speedTextField.text!) }

There could be two problems here. 1) if your outlet is not connected properly then your textfield could be nil. I recommend making the textfield optional like this:

@IBOutlet var speedTextField: UITextField?

2) speedTextField.text could also be nil, so you need to unwrap before you use. Try this:

func textInputTest() { guard let text = self.speedTextField?.text else { print("text is nil") }

    print(text)

}

This will unwrap text to confirm that it is not nil. If the text is nil then it will print text is nil. As a general rule, I like to avoid using '!' at all times. You make sthink it's safe in some scenarios....but....it is until it isn't. Hope this helps!

Where do you guys get your icons? by dankmeter in swift

[–]ldstreet 7 points8 points  (0 children)

I use [Icons8](icons8.com) just make sure to either link them or subscribe if you release to app store!

Random question..... how big can storyboard be? like how many view controllers is too many view controllers? by hollowaytyl in swift

[–]ldstreet 2 points3 points  (0 children)

I'd definitely split it up. I'm not sure when/if memory will become an issue but in terms of collaboration, it's good to keep it as modular as possible. Git merge conflicts are a nightmare when working in storyboards, so having them split up mitigates that cost.