Swift calling function displaying funcName(ViewController) instead of arguments? - ios

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.

Related

not able to declare an append command on an array in iOS

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.

Is it possible to call Gdk.Seat.grab() in GJS?

It seems when I call Gdk.Seat.grab() in GJS I get an error:
Gjs-WARNING **: JS ERROR: TypeError: Gdk.Seat.grab is not a function
This function and class is listed in the GJS Docs, but maybe I'm calling it wrong? If I call typeof on Gdk.Seat.grab it comes back undefined. Is this not possible, or is there another way I can grab focus in this way?
My use case is gathering a keybinding from a user, for which I can use Gtk.CellRendererAccel, but I would prefer not to use a Gtk.TreeView. The docs say about CellRenderers that:
These objects are used primarily by the GtkTreeView widget, though they aren’t tied to them in any specific way.
and...
The primary use of a GtkCellRenderer is for drawing a certain graphical elements on a cairo_t.
Which implies I could use it outside of TreeView, but with no hints as to how.
grab() is a method of Gdk.Seat, so you need a Gdk.Seat object to call it on. It looks like you're calling it as a static method, Gdk.Seat.grab(). So, you'll need something like Gdk.DeviceManager.get().get_default_display().get_default_seat() or you can get a Gdk.Seat object from a Gdk.Event.
It's not clear from the described use case what you are trying to do with the grab, but there may be an easier way to accomplish it.

Xcode 9 won't autocomplete functions by variable names

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!

How to run multiple functions onClick

The reason it took me forever to find out is that I don't know how it is called. But I hope if I describe the question here as thoroughly as possible, it will pop up in the search results anyways.
Possible other titles:
How can I pass a function without name as parameter to querySelector.onClick.listen?
How to use a function without a name in dart?
How to create a nameless function that you can use only exactly there where it is defined?
I know that the following works - also hosted on dartpad
void main(){ querySelector('#btn').onClick.listen((e)=>fnct()); }
void fnct(){ querySelector('#btn').text="hola"; print("test");}
This changes the text of the button to "hola" and prints "test".
But what if I don't want to define a new function just for this because I like to keep the flow when reading the code and don't like to jump from function to function needlessly?
After an hour of searching, I found this
For my own code example, it would be like this dartpad link:
void main(){
querySelector('#btn').onClick.listen((e){
querySelector('#btn').text="hello";
print("no hablo espanol");
});
}
So you can define a function on the flow by using
(param){command(); secondCommand(param);}
It is entirely possible that you can find this somewhere. But I did not with my search terms. So if any of you know what the correct search terms would have been, let me know :)

Why do all methods have the same name in delegate?

i'm starting with Swift by developing a simple application with a tableView, a request to a server and a few things more. I realized that every method inside UITableViewDelegate protocol is named in the same way (i guess it might be the same with other protocols) and the differences are made by changing the parameters passed to those methods (which are called "tableView" by the way).
I was wondering why Apple would do something like this, as it's a bit messy when i try to implement method from this protocol, as i can't start typing "didSele..." just to autocomplete with "didSelectRowAtIndexPath"; instead i have to type "tableView" to get a list of all available methods and manually search for the one whose second parameter is "didSelectRowAtIndexPath".
Everything's working fine, but just trying to know WHY could this be done this way.
Thank you so much in advice :)
PS: There's a screenshot about what i'm saying:
Swift is designed to be compatible with Objective-C. After all, almost all existing OS X and iOS APIs are in Objective-C and C (with a bit of C++ code here and there). Swift needs to be able to use those APIs and thus support most Objective-C features one way or the other. One of the most important features of Objective-C is how method calls are made.
For example, in C, a function with 3 arguments is called like this:
foo(1, "bar", 3);
You don't know what the arguments are supposed to be. So in Objective-C, the arguments are interleaved with the method name. For example, a method's name might be fooWithNumber:someString:anotherNumber: and it would be called like:
[anObject fooWithNumber:1 someString:#"bar" anotherNumber:3];
Swift now tries to be compatible with this Objective-C feature. It thus supports a form of named arguments. The call in Swift would look like:
anObject.foo(number:1, someString:#"bar", anotherNumber:3)
Often Swift method definitions are written so that you don't need to explicitly name the first argument, like:
anObject.foo(1, someString:#"bar", anotherNumber:3)
If you look up the UITableViewDelegate protocol documentation and select Objective-C you can see that all of these methods start with tableView: to designate the sender, but from then on they are very different. The list you've cited is the result of the conversion from Objective-C to Swift naming convention.
It is just naming conventions. It is the same in Objective-C. You can have a look to this page. Without these conventions it would be a complete mess.
The method name is not only the first word but also the public names of the parameters.
E.g. it the method name is not tableView() but tableView(_:didSelectRowAtIndexPath:).

Resources