New answers tagged

1

I would move more logic to an Enum CardInfo struct CardInfo { enum CardState { case expanded case collapased var nextState : CardState { return isExpanded ? .collapased : .expanded } var isExpanded: Bool { self == .expanded } } enum CardType { case annotaion case chat } ...


0

Unit Tests should not have a dependency on the application bundle at runtime which could not be injected on the init phase or by a method arguments The given Utility class has a static methods which act like a shortcuts to longer syntax calls. This methods calls directly methods on Bundle class object which is cannot be mocked. The solution is to move ...


1

Naming Swift naming conventions are listed in the API Design Guidelines. With respect to “Omit needless words” and “Strive for Fluent Usage” I would suggest to call the function func randomPair(from range: ClosedRange<Int>) -> (Int, Int)? Simplify the code Your method is fine, but can be simplified a bit. First, the return values from ...


2

Best practice is to get away from manipulating date strings yourself. And use Date types in your model. So, when you parse the JSON, use DateFormatter to convert the non-user-friendly date strings to Date objects. And when presenting the dates in the UI, use another DateFormatter to present the date in a nice, user-friendly format. I’d also suggest using ...


2

A few big picture observations: You really shouldn’t have this much animation code in your view controller. For example, rather than using view controller containment, I might do a “modal” presentation with a modalPresentationStyle of .overCurrentContext. That gets you out the child view controller containment code. And you can move your animation code ...


0

I'm not sure exactly what the code is trying to do, and I don't know Swift, so my answer is a bit of a mess. I'm assuming that: there is a date in the format of YYYY/MM/dd, so for example 2019/11/30 that date could be in one of seperate places of a data structure the end result is a date which is extracted from two seperate places and is converted into a ...


2

Is there a way to tidy up the numerous functions into something like a dictionary? Sure, there are various options. But first the functions should be made independent of maxIterations, i.e. the computation Double(currentIteration) / Double(maxIterations) should be done in the caller so that the functions simplify to private func sine010(x: Double) ->...


2

If I remember correctly, at the time that exercise had been given, using loops had not been covered, so you did very well to discover the while loop and how to use it. The point of that exercise was to show you how tedious it is to write essentially the same code for each different element: let numbers = [3, 1, 4, 2] var results = [Int]() results.append(...


4

Handle edge cases If the given numbers array is empty then the first loop will do nothing, but computedNumbers.append(numbers[count - 1] * numbers[0]) aborts with a runtime exception. The correct result in this case would be to set computedNumbers to an empty array as well. Better loops This var i = 0 while i < count - 1 { // ... i = i + 1 } ...


Top 50 recent answers are included