What am I doing wrong? When I run this code in the playground I get the random element that is supposed to appear in the array, however there's an issue when I insert this code into my workspace project I get this error:
Expression resolves to an unused l-value.
var My-Array = ["Apple","Banana","Carrot","dewberry "]
My-Array[Int(arc4random_uniform(UInt32(My-Array.count)))]
This error tells you that the value that you get from your array is left unused. Playground lets you do it, because it is meant for playing with code. In production code, however, dereferencing an array and leaving the resultant value unused is a certain sign of an error.
To fix this problem, assign the value to a variable or a constant, or consume it in some other way (e.g. print it out):
let randomFruit = My-Array[Int(arc4random_uniform(UInt32(My-Array.count)))]
or
println(My-Array[Int(arc4random_uniform(UInt32(My-Array.count)))])
Related
I'm trying to run a LLVM pass on some C code and need to remove Constants from a ConstantArray. I have a GlobalVariable with said ConstantArray as initializer.
Deleting entries works usingF->replaceAllUsesWith(llvm::UndefValue::get(F->getType())) and F->dropAllReferences() but that's not acceptable - I can't change the code using the array and it stops processing the array after encountering the first NULL value.
Is there any easy way to simply drop Constants from a ConstantArray or do I have to create a new array without the respective entries I want to remove?
I'm writing a protractor test which asserts a URL against two different browser URLs.
I tried using toMatch with a regular expression but I get an error.
Is it possible to assert an actual string value against 2 or more expected string values and see if it is equal to any of them?
expect(url1).toMatch(/this.url|www.google.com/);
Sounds like you want to use toContain() which works for finding an item in an array. So switch your assertion around because you want to pass it an array of URLs [this.url, 'www.google.com'] and assert that it will contain url1.
expect([this.url, 'www.google.com']).toContain(url1);
Note: While this should work, personally I don't like to have my values on the receiving end of the assertion i.e. toContain(url1), I would rather that be passed first expect(url1).... However, in the end it's still asserting actual vs expected values so not a huge deal in my opinion.
https://jasmine.github.io/2.0/introduction.html
I have several times used the expression anArray.[i] to access an element from an array.
but all the sudden when i try to make a very very simple function then i get an error.
let safeIndexIf anArray i =
anArray.[i]
I need to make a function that returns the ith element of the array so i thought that was an easy one but no...
The Error:
The operator 'expr.[idx]' has been used on an object of indeterminate type based on information prior to this program point. Consider adding further type constraints
Why is this not working? I dont know what the error means by that. All i know i have done something similar to this lots of times to access an element of anArray so why can't i this time?
It means the compilers type inference cannot determine that anArray is indeed an array.
The error message is suggesting you add a type annotation, you could do something like this to say that anArray is a generic array:
let safeIndexIf (anArray : array<'a>) i =
anArray.[i]
If you want to avoid type annotations you could try rewriting your function to use Array.item which has the same behaviour:
let safeIndexIf anArray i =
Array.item i anArray
I am currently having trouble filling up an array of customClass.
I try to fill it with a jsonFile. During my json parsing (using swiftyJSON) i loop and fill my array.
The problem is, at the end of my loop, it is still empty. I tested it in different ways, and here is my code:
That's the file where the problem is. In my loop I fill an Annotation, that I add with append to my array. The problem is what my print return. Here is a part of it:
It's just a small part of a huge jsonfile. And, my tmpAnnot.name is correctly printed every iteration. But when it comes to my Array, nothing.
So I'm completly lost and hope you could help me ^^
(And for the information, here is my custom class) :
And btw, I tried to print my array.count, and it's nil too
Im so sorry if the question has been posted. I couldn't find it in the entire website.
Change your JSONAnnotationList declaration to be an non-optional and assign it an empty array
var JSONAnnotationList: [UGOAnnotation] = []
You see, you have never created an array so there was nothing to be printed.
The whole point of optionals is to use them sparingly, not everywhere.
I'm having trouble with a clear statement. I've got an array that gets sized dynamically, filled, and then passed to a function to be converted to some custom objects.
After that conversion I want to clear the array. I use
Array.Clear(FileData, 0, FileData.Length)
as this thread suggests (reset-an-array-to-default-in-visual-basic). However, every time I get to that point in the script the try-catch wrapping the Clear catches an out of bounds exception on the Clear.
The array is not empty (actually it's got ~34900 items) so it's not that the array has zero length. The one thing that it might be that isn't discussed in the question I referenced above is that my array is 2 dimensional.
All that said, I'm fairly well stumped. Any help would be appreciated.
UPDATE: For those experiencing this issue, I ultimately just left the step commented out, so that instead of clearing and then setting the array to Nothing, I just set it to Nothing. Doesn't really solve the underlying issue, but (should) free up memory all the same.