This question already has an answer here:
iOS Swift Multiple dimension arrays - compiliing takes ages. What should I change?
(1 answer)
Closed 7 years ago.
I am trying to implement an array that contains multiple arrays of Integers (well, LOTS of Integers) like this...
let arrayOfRanges = [Array(0 ... 299), Array(300 ... 399), Array(400 ... 699), Array(700 ... 799), Array(800 ... 899), Array(900 ... 1199)]
but every time I type it into Xcode, Xcode hangs on indexing. I've searched and found a few older cases of people with a similar problem with Arrays and it seems to be related to Xcode having difficult inferring the type. Swift Array causing indexing issues in Xcode 6.1.1 Does anybody have any ideas as to how I can better code this? I would like...
//Sets magicNumber to the Int 411
let arrayOfRanges = [Array(0 ... 299), Array(300 ... 399), Array(400 ... 699), Array(700 ... 799), Array(800 ... 899), Array(900 ... 1199)]
magicNumber = arrayOfRanges[2][11]
Thanks for your help!
Declare your variable as an array of arrays of Int
let arrayOfRanges: [[Int]] = [Array(0 ... 299), Array(300 ... 399), Array(400 ... 699), Array(700 ... 799), Array(800 ... 899), Array(900 ... 1199)]
let magicNumber = arrayOfRanges[2][11]
There isn’t anything wrong with the posted code except that you haven’t defined magicNumber. Also
magicNumber = arrayOfRanges[2][11]
will be 411 and not 410. Refer to the attached screenshot from playground.
Related
This question already has answers here:
ruby using the "&:methodname" shortcut from array.map(&:methodname) for hash key strings rather than methodname
(3 answers)
Closed 3 years ago.
I have a rails app that does a lot of JSON parsing (ie using strings as keys rather than symbols).
I have the following code:
ad_source_ids = []
logged_one['migrated'].each { |mig| ad_source_ids << mig['id'] }
I'd like to do
ad_source_ids = logged_one['migrated'].map(&:id)
but don't think I can. What is an alternative? I'd like to removed the ad_source_ids tmp variable.
You're almost there. Try this:
ad_source_ids = logged_one['migrated'].collect { |mig| mig['id'] }
I'm new to RxSwift and reading about subjects, I tried Variable Subject. Which in turns giving Warning in console
ℹ️ [DEPRECATED] `Variable` is planned for future deprecation. Please consider `BehaviorRelay` as a replacement. Read more at: https://git.io/vNqvx
Earlier I have declared Variable like this
var searchItems = Variable<[MyClass]>([])
So i have done basic array operations from it's property called value as it was get set property like
1. self.searchItems.value.removeAll()
2. self.searchItems.value.append(items)
3. self.searchItems.value = items
Now After getting warning i changed it to BehaviorRelay like
var searchItems = BehaviorRelay<[MyClass]>(value: [])
So I got error that value is get property only.
I googled alot but can't get suitable explanations for Array operations.
I only got a code self.searchItems.accept(items) which i really don't know what it exactly do add fresh items or append.
I needed how all 4 operations will be performed when using BehaviorRelay?
1) Remove all
var array = self.searchItems.value
array.removeAll()
self.searchItems.accept(array)
2) Append item
self.searchItems.value.accept(searchItems + [items])
3) Value = ...
self.searchItems.value.accept(items)
Use accept.
var value = searchItems.value
value.removeAll()
searchItems.accept(value)
etc...
I have an C API I need to interact with in Swift.
One of the function takes an array of pointers as argument, which is imported by Swift as
`UnsafeMutablePointer<UnsafeMutablePointer<Float>?>! `
The corresponding input on the Swift side is AVAudioPCMBuffer.floatChannelData, which is defined as
UnsafePointer<UnsafeMutablePointer<Float>>?
I am having trouble casting between the two.
I tried to make it mutable by doing following:
UnsafeMutablePointer<UnsafeMutablePointer<Float>>(AVAudioPCMBuffer.floatChannelData)
And that did not work. The casting between Swift pointer types are very frustrating. Any help will be really appreciated.
Thanks
var testPtr : UnsafeMutablePointer<UnsafeMutablePointer<Float>?>! = nil
var testPtr2 : UnsafePointer<UnsafeMutablePointer<Float>>? = nil
testPtr = unsafeBitCast( testPtr2, to: UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!.self)
As per excellent #easytarget suggestion since XCode 13.3 use modern syntax instead (not generating a warning):
testPtr = UnsafeMutablePointer(mutating: testPtr2)
The following code used to work, but now when I try and use the code I'm getting an error stating, "Ambiguous reference to member 'append'". Any ideas?
var allInfo: Array = [[String]]()
let Dogs : Array = ["Border Collie","Doberman", "German Shepherd"]
let Cats : Array = ["Top Cat","Tom"]
allInfo.append(Dogs)
allInfo.append(Cats)
I've changed the name of the allInfo variable to myPets to see if it that was the problem and updated to Xcode 7.1.1 but still getting the following error. Although it works in playground. Very frustrating.
Thanks for all the support. I got this working by explicitly adding the type of each array as follows:
let Dogs : Array<String> = ["Border Collie","Dobermann", "German Shepherd"]
And I had to remove the : Array from my multidimensional arrays like so:
var myPets = [[String]]()
I don't know if this is an Xcode bug but hopefully my answer may help others in our community.
You most likely have a different definition of either the allInfo variable or the elements you are adding. Make sure you do not have duplicates of those.
Hi all real newbie question here.
I have an array like this:
var daysInMonth = Array<([MyCustomClass], NSDate)>()
How can I append an element to this?
I am having difficulty doing so. Trying something like this:
daysInMonth.append([MyCustomClass](), someDate)
or
daysInMonth.append( ([MyCustomClass](), someDate) )
will not work (i'd like to add an empty array initial above of type MyCustomClass, as well as some date that I have) but these are failing (error missing parameter #2 in call)
Any thoughts on what I am lacking in my syntax?
Thanks!
It looks like a swift bug to me. The swift compiler cannot correctly parse the "( (...) )" as passing in a tuple to a function.
If I break the append operation into two statements, it works.
var daysInMonth = Array<([MyCustomClass], NSDate)>()
let data = ([MyCustomClass()], NSDate()) // assuming MyCustomClass init() taks no parameter
daysInMonth.append(data)
note: It was [MyCustomClass]() in your question, which is incorrect.
Try declaring your array using the newer Array syntax instead:
var daysInMonth = [([MyCustomClass], NSDate)]()
Then, this works:
daysInMonth.append(([MyCustomClass](), NSDate()))