Appending elements to an array - ios

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()))

Related

RxSwift : BehaviorRelay in place of Variable usage

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...

How can I cast UnsafePointer<UnsafeMutablePointer<Float>>? to UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!

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)

Multidimensional Arrays Ambiguous Reference to Members

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.

Array.size() returned wrong values (Grails)

I'm developing an app using Grails. I want to get length of array.
I got a wrong value. Here is my code,
def Medias = params.medias
println params.medias // I got [37, 40]
println params.medias.size() // I got 7 but it should be 2
What I did wrong ?
Thanks for help.
What is params.medias (where is it being set)?
If Grials is treating it as a string, then using size() will return the length of the string, rather than an array.
Does:
println params.medias.length
also return 7?
You can check what Grails thinks an object is by using the assert keyword.
If it is indeed a string, you can try the following code to convert it into an array:
def mediasArray = Eval.me(params.medias)
println mediasArray.size()
The downside of this is that Eval presents the possibility of unwanted code execution if the params.medias is provided by an end user, or can be maliciously modified outside of your compiled code.
A good snippet on the "evil (or lack thereof) of eval" is here if you're interested (not mine):
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/
I think 7 is result of length of the string : "[37,40]"
Seems your media variable is an array not a collection
Try : params.medias.length
Thanks to everyone. I've found my mistake
First of all, I sent an array from client and my params.medias returned null,so I converted it to string but it is a wrong way.
Finally, I sent and array from client as array and in the grails, I got a params by
params."medias[]"
List medias = params.list('medias')
Documentation: http://grails.github.io/grails-doc/latest/guide/single.html#typeConverters

Deedle - create empty list and series

I am new to F#, looking at it as an alternative to Matlab.
In reference to this question, how can I create an empty Serie and an empty Frame.
If I did not just miss it, why an empty Serie or Frame has not been designed in the library,
something like list.empty ?
Adding Frame.empty and Series.empty is a great suggestion. I think these should be in the library and I'm adding an issue to GitHub to make sure they get added.
In the meantime, you should be able to use something like this:
let empty : Series<int, float> = series []
let empty : Frame<int, string> = frame []
Note that I had to add type annotations - from my code snippet, the compiler cannot figure out what is the type of keys and values, so I had to explicitly specify that (but if you use the values as arguments to other functions, then this should not be needed).
The second line does not actually work in the current version, because of a bug (oops!) so you can use Frame.ofRows instead, which works fine:
let frame : Frame<int, string> = Frame.ofRows []
EDIT: The bug is now fixed in version 0.9.11-beta

Resources