Can a Swift init have a variadic parameter at the end so you can send multiple values of that type to the init?
An example would be to create a class that has an array of UIViews. Would the following work? Is it considered "legit" to do this? (I know I could just pass an array of views, just wondering if this is an option.)
class viewsContainer {
var myViews: [UIView] = []
init(views: UIView...) {
for view in views {
myViews.append(view)
}
}
}
Yes, it's a valid approach, but you should set different frames to not overlap the subviews.
Perfectly legal. I just think it's best to pass an array of views instead (so myViews can be a constant), though. You could create a temporary array with the variadic parameter, and assign it to a private constant, but you, know...
Related
Given two arrays of RandomModelObject that conform to Codable, Equatable and Hashable I want to calculate a diff between them and animate content changes in a UICollectionView. Having to support iOS 11 made me pick https://github.com/tonyarnold/Differ as a dependency for doing so.
This code:
class ScreenNameCollectionViewDataSource {
var elements: [RandomModelObject] = []
}
extension ScreenNameViewController: ScreenNameViewModelDelegate {
func elementsStoreUpdated() {
collectionView.animateItemAndSectionChanges(oldData: dataSource.elements,
newData: viewModel.blablabla,
updateData: {
dataSource.elements = viewModel.blabla
})
}
}
Produces 2 errors:
Instance method 'animateItemAndSectionChanges(oldData:newData:indexPathTransform:sectionTransform:updateData:completion:)' requires that 'RandomModelObject.Element' conform to 'Equatable'
Instance method 'animateItemAndSectionChanges(oldData:newData:indexPathTransform:sectionTransform:updateData:completion:)' requires that 'RandomModelObject' conform to 'Collection'
The errors don't seem to point me anywhere - Array is a Collection and the model conforms to Equatable. Did I miss anything there?
You are using animateItemAndSectionChanges, which not only requires that T is a Collection, but it also requires that T's elements are Collections. In other words, T needs to be something like a 2D array.
This is because animateItemAndSectionChanges handles both rows and sections. The 2D collection will tell the method what the old and new rows and sections are. Each "inner" collection represents a section.
Since your data source is a 1D array, it seems like you just need animateRowChanges, which is for single-section table views.
If each RandomModelObject actually represents a section, then you need to map each one of those to an array, so that you get a [[Something]], and change the updateData closure accordingly.
When using Objective-C I would pass a NSMutableArray from one view controller VC_A to another VC_B by simply assigning a property in VC_B as
VC_B.list = self.list
where self is VC_A
It allows the changes done in VC_B on the list to be seen in the list in VC_A when the view controller was say popped off the navigation stack.
However in Swift as arrays are passed by value, assigning as above does not work so I am stuck how to solve this. What would be the correct way to handle this now?
You can still do this in Swift by making the property an NSMutableArray, just as before. Foundation types still exist if you ask for them. But this is bad design in both ObjC and Swift. It creates spooky action at a distance (when things magically change values that were not part of the call) and likely breaks MVC (if the data is persistent, it should live in the model, not in the view controllers).
There are two common patterns in Cocoa: store the data in the model, or pass it via delegation.
If this array represents some kind of persistent state (such as a list of items in the system), then that belongs in the model layer, and both view controllers should read and manipulate it there rather than by communicating with each other. (See Model-View-Controller.)
If this array is a transient piece of data (such as selections from a list), then the calling VC should set itself as the delegate to the receiving VC, and when the receiving VC finishes, it should pass the data back to its delegate. (See Delegates and Data Sources.)
If you use the standard Swift Array which is a value type you have to use a wrapper or a untyped NSArray.
// a generic wrapper class
class Reference<T> {
var value: T
init(_ val: T) { value = val }
}
// Usage
class ViewController1 {
static var list = Reference<[Int]>([])
}
class ViewController2 {
static var list = Reference([3, 5, 7, 9, 11])
func passToOtherVC() {
ViewController1.list = self.list
}
}
If you want to mutate the array you should always change the value property of the Reference object.
In Swift, objects are automatically passed by reference. NSArray is an Objective C class (pass by reference), where as Array is a struct (pass by value).
So if you are working with NSMutableArray the array is already being passed by reference.
Just as a potential proof of concept that complements my comment on the question - it is possible to use the Objective-C NSMutableArray to accomplish this task:
class A {
var x: NSMutableArray = NSMutableArray(capacity: 12)
}
class B {
var y: NSMutableArray!
}
let a = A()
let b = B()
b.y = a.x
b.y[0] = 123
assert(a.x[0] === b.y[0])
Still, this is approach is not following the Swift style of handling data structures IMO.
When I try to create an array of UIButtons in the ViewController
Instance member 'tile11' cannot be used on type 'ViewController'
It works when I create it inside a method, but I need the array in more than one method, and it's getting annoying to having to create the array again and again.
class ViewController: UIViewController {
let tileArray: Array<UIButton> = [tile11, tile12, tile13, tile21, tile22, tile23, tile31, tile32, tile33]
}
There isn't enough code to tell for sure, but I'm guessing tile11 and the other tiles are other instance variables (button outlets to be specific)? They would technically not be accessible until run-time. This is why you cannot instantiate tileArray like that, but why it works in a method. The only way to instantiate tileArray in line with the declaration is to use constants in the array. You should initialize tileArray in viewDidLoad().
When using Objective-C I would pass a NSMutableArray from one view controller VC_A to another VC_B by simply assigning a property in VC_B as
VC_B.list = self.list
where self is VC_A
It allows the changes done in VC_B on the list to be seen in the list in VC_A when the view controller was say popped off the navigation stack.
However in Swift as arrays are passed by value, assigning as above does not work so I am stuck how to solve this. What would be the correct way to handle this now?
You can still do this in Swift by making the property an NSMutableArray, just as before. Foundation types still exist if you ask for them. But this is bad design in both ObjC and Swift. It creates spooky action at a distance (when things magically change values that were not part of the call) and likely breaks MVC (if the data is persistent, it should live in the model, not in the view controllers).
There are two common patterns in Cocoa: store the data in the model, or pass it via delegation.
If this array represents some kind of persistent state (such as a list of items in the system), then that belongs in the model layer, and both view controllers should read and manipulate it there rather than by communicating with each other. (See Model-View-Controller.)
If this array is a transient piece of data (such as selections from a list), then the calling VC should set itself as the delegate to the receiving VC, and when the receiving VC finishes, it should pass the data back to its delegate. (See Delegates and Data Sources.)
If you use the standard Swift Array which is a value type you have to use a wrapper or a untyped NSArray.
// a generic wrapper class
class Reference<T> {
var value: T
init(_ val: T) { value = val }
}
// Usage
class ViewController1 {
static var list = Reference<[Int]>([])
}
class ViewController2 {
static var list = Reference([3, 5, 7, 9, 11])
func passToOtherVC() {
ViewController1.list = self.list
}
}
If you want to mutate the array you should always change the value property of the Reference object.
In Swift, objects are automatically passed by reference. NSArray is an Objective C class (pass by reference), where as Array is a struct (pass by value).
So if you are working with NSMutableArray the array is already being passed by reference.
Just as a potential proof of concept that complements my comment on the question - it is possible to use the Objective-C NSMutableArray to accomplish this task:
class A {
var x: NSMutableArray = NSMutableArray(capacity: 12)
}
class B {
var y: NSMutableArray!
}
let a = A()
let b = B()
b.y = a.x
b.y[0] = 123
assert(a.x[0] === b.y[0])
Still, this is approach is not following the Swift style of handling data structures IMO.
I have declared a Swift protocol:
protocol Option {
var name: String { get }
}
I declare multiple implementations of this protocol—some classes, some enums.
I have a view controller with a property declared as so:
var options: [Option] = []
When I try and set this property to an array of objects that implement the Option protocol in another VC's prepareForSegue, I get a runtime error:
fatal error: array cannot be bridged from Objective-C
Why doesn't this work? The compiler has all the information it needs, and I don't understand what Objective-C has to do with it at all—my project contains only Swift files, and these arrays aren't coming in or out of any framework methods that would necessitate them being bridged to NSArray.
I have found a solution. It is quite... unsatisfying, but it works. Where I set the array on the destination view controller I do:
destinationViewController.options = options.map({$0 as Option})
the compiler knows I'm passing in an Array of things that implement Option
You've let slip there a very revealing remark, which suggests the source of the issue. An "Array of things that implement Option" is not an Array of Option.
The problem is with the type of options back at the point where you create it (in prepareForSegue). You don't show that code, but I am betting that you fail to cast / type it at that point. That's why the assignment fails. options may be an array of things that do in fact happen to adopt Option, but that's not enough; it must be typed as an array of Option.
So, back in prepareForSegue, form your options like this:
let options : [Option] = // ... whatever ...
Now you will be able to assign it directly to destinationViewController.options.
Here's a quick test case (in a playground; I detest playgrounds, but they can have their uses):
protocol Option {
var name : String {get}
}
class ViewController : UIViewController {
var options : [Option] = []
}
enum Thing : Option {
var name : String {
get {
return "hi"
}
}
case Thing
}
let vc = ViewController()
let options : [Option] = [Thing.Thing]
vc.options = options // no problem
(I also tested this in an actual app with an actual prepareForSegue, and it works fine.)
I was having the same problem and fixed it marking my protocol with #objc, in your case it would look like this
#objc protocol Option {
var name: String { get }
}
Got the solution from this answer
This one also works fine
destinationViewController.options = options.map{$0}