I have a UITableViewController that can display content from any of three different arrays. In Objective-C, what I would typically do is set a property on the TableVC that is a reference to the array to be displayed. Then the TableVC would not only be able to show the contents of that array, but also handle user-directed editing of the array, say, if they deleted or re-ordered elements.
The way it seems to work in Swift when I set a property on my TableVC to an array is that it is a copy of my model's array, not a reference. And while this is fine in certain contexts, in my application it seems bad for two reasons: 1) my arrays are enormous, thousands of elements, and copying huge arrays over and over again seems wasteful; 2) small edits are tougher to handle: I have to communicate back to the model about element deletion or re-ordering and make sure my view and model arrays stay in sync.
Setting a property that's a reference to an array is what I want to do, but there's no such thing as an inout object property in Swift.
What is the correct way for me to handle this?
Thanks.
Swift Arrays are not actually copied every time you assign them; instead, they are copy-on-write. What this means is that the array is only copied when something mutates it; until that point, you only copy a reference, like with Objective-C.
With that said, if you need reference semantics, the NSArray class from Objective-C is still available in Swift. You can also create your own class type that wraps an Array if you prefer not to use the Objective-C bridge.
Related
I have an array which contains objects. These objects may have other arrays of objects, then those objects also may have other objects and array of objects. There is no limit really.
So how do I go through all the objects in this array no matter how deeply nested they are? If I know how deep the arrays go, I can easily do this by using a number of for loops. I having problem with going through all the elements.
I am not able to come up with a logic to do that. Can somebody please help me? I am trying to do this in swift.
Have an Any object.
Store the values into that object how you need it.
When you read the object, cast it to the required array.
If the casting is successfull, then you can access that Any object like an array.
I have a dictionary like [String:[MyClass]] and I want to access each value in each array in the dictionary, but without any copying.
Can I do this with for..in? Or maybe the forEach method does the job?
You can just iterate normally with for..in and not worry about the value copying mechanics. If you are interested in what's happening under the hood, look up copy-on-write behaviour, which Swift arrays adopt.
The gist of it is: A copy of the array (or any other data structure that implements copy-on-write) will only be created in memory when you modify that array. If it doesn't mutate, the two array instances will still use the same underlying memory.
So I've been learning crystal without a ruby background and noticed the api docs have #dup and #clone for basically copying an array.
What exactly is the difference between the two? The api says #dup shallow copies the array whilst #clone deep copies said array. I'm not sure what exactly that entails and which one I should be using.
#dup will duplicate the array in memory, that is the list of items it contains, but it won't duplicate the items themselves. Mutating the new array won't affect the previous array(e.g. push, pop) but mutating any of its items will affect the item of the previous array since the items are the same objects.
#clone will duplicate the array list in memory, but also its items, by calling #clone recursively. This is a full clone of the original array. Mutating anything, even a deep nested object, won't affect the original content.
Note that this applies to any object, not just arrays, and that the behavior can be customised by overriding the methods in your own objects.
That being said, it only applies for arrays of objects (e.g. class instances). For arrays of primitives (integer, float, struct...) the items will be copied along with the array list by #dup.
I am wishing to associate object methods with dtypes in a numpy structured array. That is the underlying object instance state of my collection of object is already packed into a numpy (nested dtype) record_arrays..
I already have a set of Python objects, that are constructed on top of views of these numpy structured array dtypes that then operate on this underlying continuously packed object instance data.
That is: I am wishing to use object oriented programming methods, while working with the underlying instance state in it's native packed, structured format.
-> Currently working with these numpy structured arrays using Python object methods, requires that explicitly instantiate a Python object on top of it's associated array view each time I re-reference it within the array..
Might it be possible to more directly associate object methods with a numpy structured array (nested) dtype, so that one could avoid having to reconstruct the object instance each time ??
Your description is confusing. Some basic code might help make it clearer. But I'll throw out some possibly relevant observations.
A numpy array has a (large) continuous data buffer (flat list of bytes), which it divides into 'records'. 'shape' and 'strides' are used to step through the records. 'dtype' determines how it 'views' each record.
While these arrays are a Python object type, and dtype is also a class, in general numpy programmers don't put a lot of effort into constructing added array object classes. The arrays may be attributes of larger objects (dictionaries, lists, tuples, or custom classes).
matrix and masked_arrays are examples of subclassing ndarray. I have not seen many user defined subclasses; in part because the amount of work to make them fully functional isn't worth it. recarray is a subclass that allows you to access fields of a structured array with attribute syntax. It may be worth looking at its 'getattr` method. Getting general array operations to return the correct array class or subclass is a bit tricky.
np.lib.index_tricks defines a few classes that let you use indexing syntax inplace of function syntax (ie. they define custom __getitem__ methods).
I am not aware of ways to subclass dtype. The most general base dtype is 'object'. That just stores a pointer to a Python object that is stored elsewhere. That pointer can point to anything - a number, None, a list, another array, etc. In structured arrays, dtype is a compound type, consisting of 'list' of sub-dtypes (which may be nested). But ultimately the structured dtype consists of multiple basic dtypes (ints, floats, strings, object).
So in Swift, what's the difference between
var arr = ["Foo", "Bar"] // normal array in Swift
and
var arr = NSMutableArray.array() // 'NSMutableArray' object
["Foo", "Bar"].map {
arr.addObject($0)
}
other than being different implementations of the same thing.
Both appear to have all the basic features that one might need (.count, the ability to insert/remove objects etc.).
NSMutableArray was invented back in the Obj-C days, obviously to provide a more modern solution instead of the regular C-style arrays. But how does it compare to Swift's built-in array?
Which one is safer and/or faster?
The most important difference, in my opinion, is that NSMutableArray is a class type and Array is a value type. Ergo, an NSMutableArray will be passed as a reference, whereas a Swift Array will be passed by value.
Furthermore NSMutableArray is a subclass of NSObject whereas Array has no parent class. - this means that you get access to all NSObject methods and other 'goodies' when utilising NSMutableArray.
An NSMutableArray will not be copied when you amend it, a Swift Array will be.
Which one is best really depends on your application.
I find (when working with UIKit and Cocoa touch) that NSMutableArray is great when I need a persistent model, whereas Array is great for performance and throw away arrays.
These are just my initial thoughts, I'm sure someone from the community can offer much deeper insight.
Reference Type When:(NSMutableArray)
Subclasses of NSObject must be class types
Comparing instance identity with === makes sense
You want to create shared, mutable state
Value Type When: (Swift array)
Comparing instance data with == makes sense (Equatable protocol)
You want copies to have independent state
The data will be used in code across multiple threads (avoid explicit synchronization)
Interestingly enough, the Swift standard library heavily favors value types:Primitive types (Int, Double, String, …) are value types
Standard collections (Array, Dictionary, Set, …) are value types
Aside from what is illustrated above, the choice really depends on what you are trying to implement. As a rule of thumb, if there is no specific constraint that forces you to opt for a reference type, or you are not sure which option is best for your specific use case, you could start by implementing your data structure using a value type. If needed, you should be able to convert it to a reference type later with relatively little effort.
Conclusion:
Reference types incur more memory overhead, from reference counting and also for storing its data on the heap.
It's worth knowing that copying value types is relatively cheap in Swift,
But it’s important to keep in mind that if your value types become too large, the performance cost of copying can become greater than the cost of using reference types.