mutate swift array from objc?
How about:
@objc class Foo: NSObject {
var myArray = [Bar]()
func addBar(newBar: Bar) {
myArray.append(newBar)
}
}
then
Foo* foo = [[Foo alloc] init];
[foo addBar: bar];
According to the Law of Demeter this is the better way to write it, as you're not accessing a method on a property of foo
how to mutate inputting array in-place in swift?
An Array
of Int
is a value type, that means the object is copied while being passed to the method.
You can declare the arr1
parameter as inout
, that treats the array as reference type:
private func a(inout arr1: [Int]) {
arr1.removeRange(0..<2)
}
var array = [1, 2, 3, 4]
a(&array)
print(array) // [3, 4]
or you have to return the changed array:
private func a(var arr1: [Int]) -> [Int] {
arr1.removeRange(0..<2)
return arr1
}
let array = [1, 2, 3, 4]
let result = a(array)
print(result) // [3, 4]
Mutable Swift array becomes NSArray when I initialize with Obj-C
In swift, the difference between mutable and immutable array is;
var books:[Book] // is a mutable array
let books:[Book] = [book1, book2]; // is immutable array due to let
but I don't think, same rule is followed when bridging to ObjC.
Just for a fix, you may have mutableArray specifically.
import Foundation
@objc public class Model : NSObject {
var books:NSMutableArray = NSMutableArray();
override init(){
super.init();
// other code
}
}
You will need to parse the values to Book Class when retrieving from the array.
How to cast a swift array of tuples to NSMutableArray?
Since swift types like Tuple
or Struct
have no equivalent in Objective-C they can not be cast to or referenced as AnyObject
which NSArray
and NSMutableArray
constrain their element types to.
The next best thing if you must return an NSMutableArray from a swift Array of tuples might be returning an Array of 2 element Arrays:
let itemsTuple = [("Pheonix Down", "Potion"), ("Elixer", "Turbo Ether")]
let itemsArray = itemsTuple.map { [$0.0, $0.1] }
let mutableItems = NSMutableArray(array: itemsArray)
Mutating error when attempting to append an array of a struct
The issue is that a struct
cannot change its own properties without the function that is changing those properties being marked mutating
. You can’t mark body
as mutating
, but you can make children
a @State var
. @State
variables are mutable, but only from within your view’s body
property.
Objective-C NSMutableArray mutated while being enumerated?
It's nothing to do with your timers. Because (I assume) your timers are all working on the same thread as your modifying method you don't need to stop and start them. iOS doesn't use an interrupt model for timer callbacks, they have to wait their turn just like any other event :)
You're probably doing something like
for (id object in myArray)
if (someCondition)
[myArray removeObject:object];
You can't edit a mutable array while you're going through it so you need to make a temporary array to hold the things you want to remove
// Find the things to remove
NSMutableArray *toDelete = [NSMutableArray array];
for (id object in myArray)
if (someCondition)
[toDelete addObject:object];
// Remove them
[myArray removeObjectsInArray:toDelete];
How can I change the element in an array of struct
Pair
is a struct, so value semantics apply. This means that here, you made a copy of each item in data.pairs
:
var pair = data?.pairs[index]
And you are changing the copies, not the originals.
One way to solve this is to assign the changed copies back to data.pairs
:
if /* some test */ {
print ("Found match")
pair?.setValue(Point(1,2))
data?.pairs[index] = pair // here!
}
Or, don't make a copy in the first place:
if /* some test */ {
print ("Found match")
data?.pairs[index].setValue(Point(1,2))
}
Or, make Pair
a class, so reference semantics apply, and you won't be making copies.
Related Topics
Using Avaudioplayer to Play Remote Mp3 File in Swift
Save Struct in Class to Nsuserdefaults Using Swift
Turn Swift Object into a JSON String
How to Use Urlsession with Proxy in Swift 3
Uicollectionview Cells with Images Inside Uitableview Prototype
Format Currency in Textfield in Swift on Input
Uitextview - Adjust Size Based on the Content in Swiftui
How to Create a "Hyperlink" with Swift
How to Catch Nsunknownkeyexception in Swift 2.2
How to Make Playground Execution Time Is as Fast as If We Run in iOS Application
Uitraitcollection Clarification
Sorting Struct Array in Swift 4
How to Change Status Bar Text Color Immediately in Swift in iOS 13
Programmatically Obtaining the Imei or Udid of an iOS Device
Nsexceptionallowsinsecurehttploads Not Working for Ip Addresses
Pass Data from Tableview to Tab Bar View Controller in Swift