when trying to append a value to first initialize the array in the code body
var aValue = [Double]()
var bValue = [Double]()
aValue.append(0.0)
bValue.append(0.0)
the program tells me that they are not allowed above the class declaration
if trying to move it under the class declaration, it won't work either, and when trying to put it under its own button, only one of the "appends" works, but the other doesn't
I just tested this in a new iOS project and it works fine. I'm guessing you either aren't using the standard tools (and aren't getting the Swift Standard Library somehow?), or for some reason it wants you to import Foundation, which shouldn't be necessary.
The only other thing I can think of is someone has overridden append in your project, possibly on an extension that explicitly extends Array<Double> and you're somehow in conflict with that.
Related
I'm trying to build a iOS framework but at the moment I'm trying to import the framework I'm getting this error:
This is implementation of my framework:
public protocol myframeWorkDelegate{
func doSomething(value:Int)
}
public class myframeWork{
public var delegate:myframeWorkDelegate? = nil
public func doingSomething(do:String){
}
}
Any of you knows why I'm getting this error?
I'll really appreciate your help.
This happens because you named your variable the name of your class. Swift compiler starts using the name of your variable right away, and decides that you are trying to read the value inside its own initializer, as in
var someVariable : String = someVariable
Of course, you are not doing this, so Swift compiler could distinguish between the two uses of myframeWork identifier in the declaration, at least theoretically. However, they decided it is not worth the trouble.
Renaming the variable or the class will fix this problem. You may also need to provide a public initializer for your class in order for the modified code to compile.
Change the name of your var. The error is because the name of the class is the same as the name of the variable. Additionally, this is why you should always capitalize class names ie class MyFramework versus var myFramework.
We all know and love the autocomplete feature of Xcode.
The above screenshot is taken from Xcode 9. I looks identical to what it did in Xcode 8. It knows about my class, and all of its different declarations and functions etc. This is not a SearchPaths-problem.
In Xcode 8, we were able to start typing the function name or the name of any variable used in the declaration of any function/initialiser to help the autocomplete single out which we want, like this:
However, in Xcode 9 this no longer happens. Instead, it completely ignores context and starts to show autocompletion as if I was typing this on a new line.
Is there a way to enable this again? I didn't know I needed this function until I lost it.
This happens to me in XCode 9.3, but only (it seems) if all the following conditions are met:
The instance is created from a different file or scope than the type definition
The instance is not created from within a function
An instance of the same type has not already been created at the current scope. (As discussed here.)
This implies some possible workarounds. You can create the instance within a function and then move the line of code elsewhere. Or you can create a dummy instance first, followed by the real instance on the next line—this works even if you don't include the arguments on the dummy line. For example:
let dummy = MyObject // no autocomplete available here
let obj = MyObject(anything: Any Object) // autocomplete working on this line!
i am currently doing an app that has language change feature. However,for every string that i wish to have changes made to when a different language is selected, i have to implement the following code
"Hello world".localize()
However, as the app gets bigger, the code become very messy in the way that all the strings in their respective view controllers have this .localize() append to it.
Is there a way where i can do this .localize() thing in one central place?
EDIT: I tried to create a Strings.swift file and put all the strings inside. I did something like this.
static let relevantString = "hello world".localize()
and then in the view controllers i call
let myString = relevantString
However, this does not well. The strings will only change after i terminate the app and restart it.
Your attempt to use static let fails to produce the dynamic behaviour you want simply because you used a constant. You could use a read only computed property instead, something like:
var relevantString : String { return "Hello World".localize() }
As an alternative, as you seem to be more concerned over the clutter, you could define a simple prefix or postfix unary operator which called localize() on its argument. Swift allows a number of symbols to be used as operators so the operator can just be a single character. For example you could define, say, § so that §"Hello World" produced a localised string.
HTH
Trying to access a private (package internal) field got me in a strange situation.
My Class "Properties" has a internal field named '_forceAccum'.
Trying to get the value of it fails for me using this code:
InstanceMirror bodyMirror = reflect(props);
var value = propsMirror.getField(new Symbol('_forceAccum'));
but if I use this instead:
InstanceMirror bodyMirror = reflect(props);
var value = propsMirror.getField(new Symbol('_forceAccum#434525364'));
it works. (I got the "#..." from iterating through the symbols (.toString()) from the class mirror).
Is it supposed to work this way? Is it safe or will it change in the next version? (I'm using 1.7.2)
or does it just work by pure happenstance?
Your problem is that you have a library private name. It's name isn't just _forceAccum, it's a special symbol related to _forceAccum that is unique to the library, but different from a _forceAccum in another library.
To create the private symbol using mirrors, you must use MirrorSystem.getSymbol as: MirrorSystem.getSymbol("_forceAccum", libraryMirrorOfLibrary). This creates a library-private symbol for that particular library. You can't create a private symbol without either specifying the library that it's private to, or by being in the library (#_forceAccum will create the correct symbol for the library that it occurs in).
The other alternative, which you used, is to look through the library and find the already existing symbol there:
var forceAccumSymbol =
libraryMirrorOfLibrary.declarations.keys
.where((n) => MirrorSystem.getName(n) == "_forceAccum")
.first;
(if the declaration is a top-level declaration).
The _forceAccum#something name that you are seeing is the VM's way of making a name library private. It's not going to work if compiled with dart2js, and it's probably not going to have the same something on each run, so you can't can't put it into your code.
The behavior is not guaranteed.
If you were in the same library you could probably build the symbol yourself (either with #_forceAccum or with const Symbol("_forceAccum") or new Symbol("_forceAccum")).
The best way to get to the symbol from outside its library is probably to find it (testing with toString() and then cache it.
In swift i was messing around with some functions in the playground and figured out what I needed to do. So i went ahead and pasted this function into my viewController.swift file however when I went to assign a variable it did this:
(incase the images goes)
This is what autocomplete suggests when I call my function which has 5 arugments in my viewController.swift
let test = template(<#ViewController#>)
instead of this which is what it was doing in the playground
(incase images goes)
This is what the autocomplete suggests when i call it in the playground (each object between the <> i can just tab through and change)
let test = template(<question: String>, <answerOne: String>, <answerTwo: String>, <answerThree: String>, <answerFour: String>, <correctAnswer: Int>)
The only reason I ask is because it was so much easier in the playground because I could easily just hit enter and then tab through each value that needed adding, now it takes much longer, especially when I have to do it 500+ times. Is there something I am doing wrong or anyway I can make my viewController.swift behave like it did in the playgroud? Also the function has a "m" instead of "f" if you look at the pictures on the autocomplete.
EDIT:
Thanks to Alblu I realised the stupid error I was making. I was trying to declare the variable straight under the function (as in not inside any other method). When I went to declare this in the viewDidLoad method it worked perfectly. Rookie error.
You're trying to assign the variable to something of the same name, and as a result, Xcode is getting confused between what you mean as a variable and what you mean as a function call. One way of solving this is to have different names for your variables and your functions.