what is this new "in" keyword in swift 3 [duplicate] - swift

This question already has an answer here:
What are the new "for", "at", "in" keywords in Swift3 function declarations?
(1 answer)
Closed 6 years ago.
I have just opened a swift 2 project with last Xcode version.
Xcode suggest me to auto convert my code to swift 3.
Here is something strange:
func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
func tableView(_ tableView: MyTableView, numberOfRowsInSection section: Int) -> Int
{
return 10
}
If you look, Xcode added a "in" keyword for the numberOfSections method. But he did nothing for the numberOfRowsInSection method.
I do not understand why.
What is this "in" keyword in Swift 3 ?

It's not a keyword. It's the name of the first parameter in the method.
In Swift2 the method was...
func numberOfSections(inTableView: UITableView) -> Int
... and it would be called like...
numberOfSections(tableView)
In Swift 3 there is a lot of emphasis on making function names more verb based and removing redundant inferred or implied type from the method names. Also, the first parameter name of methods is now required.
All these changes lead to the name being updated to what you have seen and the call becomes...
numberOfSections(in: tableView)
You can read more about the changes in Swift 3 in Apple's API Guidelines.

Related

Swift (IOS) Update parameter in async function

I'm currently working on creating a framework for IOS. My framework has to send request to my server, parse the response and then update the UITableView of the main page of the App.
I've already create a Singleton class "MyUpdater" which has a static func "addRow"
In my Application i have 2 objects: the tableView and a list of objects that i use for the tableView.
I want the "addRow" method (of my FrameWork) to add an object to the object list of the App asynchronously.
So i created the following method:
public static func addRow(tableView: UITableView, list: inout Array<Product>){
var my_object = Object()
list.insert(my_object, at: 1)
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: 1, section: 0)], with: .automatic)
tableView.reloadData()
tableView.enUpdates()}
And i get the following error: escaping closures can only capture inout.
How can i asynchronously update the list of the Application from the pod (framework) ?
Thank you.
Kind regards
why are you using inout ? I think this makes no sense here, since inout isn't a pass-by-reference.
Did you try something like: ?
public func addRow(tableView: UITableView, list: Array<Product>){
Dispatch.main.async {
//update the list + tableview
}
}

Swift 3 UITableViewAction error Missing argument for parameter rawValue in call [duplicate]

This question already has answers here:
Swift 3: UITableViewRowActionStyle() "Missing Parameter" Error Msg
(2 answers)
Closed 6 years ago.
I have the following code4 which worked in Swift 2 but does not work on upgrading the code to Swift 3 / Xcode 8.2
Error: Missing argument for parameter rawValue in call
Here is my problem code. The error is triggered at line 2.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
return [UITableViewRowAction(style: UITableViewRowActionStyle(), title: "Remove", handler: { (_, _) -> Void in
// return [UITableViewRowAction(style: UITableViewRowActionStyle(), title: "Remove", handler: { (_, _) -> Void in
self.tableView(self.shoppingCartTableView, commit: .delete, forRowAt: indexPath)
})]
}
UITableViewRowActionStyle is enum.
Code below should work:
UITableViewRowActionStyle.default
or just
.default

Swift 3 UIActivity Subclass problems with override

I just upgraded from swift 2 to swift 3 and I am running into some problems with overriding a property.
In swift 2 you could set the activity type like this
override func activityType() -> String? {
return "com.a.string"
}
However in swift 3 (in Xcode 8 beta 6) they introduced NS_STRING_ENUM and now we override activityType like this
override var activityType() -> UIActivityType? {
return UIActivityType.customActivityType
}
The problem is that Xcode will complain with this:
Property cannot be an #objc override because its type cannot be represented in Objective-C
One solution i found is to add the annotation #nonobjc to it like so:
#nonobjc override var activityType() -> UIActivityType? {
return UIActivityType.customActivityType
}
While this helps make the error go away, this property never gets called...
This is a problem because when the user completes the activity and completionWithItemsHandler() gets called, the activity type is considered nil.
One work around I found is to use an objective-c extension. It works; it gives me type "horse" like I wanted.
#interface Custom_UIActivity (custom)
- (UIActivityType)activityType;
#end
#implementation Custom_UIActivity (custom)
- (UIActivityType)activityType {
return #"horses";
}
My question is how to do it in a pure swift.
In pure Swift3 I am able to compile it using
override var activityType: UIActivityType {
return UIActivityType(rawValue: self.customActivityType.rawValue)
}

Cannot invoke function function with no arguments

I am still new to iOS programming and trying to understand the logic behind the program. I am trying to call a function inside of the tableView function. I just want a piece of code inside of the tableView to run at a certain point. For a simple example
override func viewDidLoad() {
tableView.testFunction()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
func testFunction() {
println("test")
}
...
}
But I get the error that I cannot invoke function with no arguments. I am new to swift so I apologize for the very basic question.
One function cannot call a function that is inside another function.
You have defined testFunction() as a local function inside your tableView:cellForRowAtIndexPath: function. No other function can see it; it is visible only inside tableView:cellForRowAtIndexPath: (and only to later code).
See my book:
A function declared in the body of a function (also called a local function) is available to be called by later code within the same scope, but is completely invisible outside its scope.

Apple Swift: what does the second argument in this function mean

In the following function:
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 10
}
is the second argument the result returned by calling the function numberOfRowsInSection with section as the argument? If so, where is it getting section from?
This syntax allows to set a name for an argument that is different from the name of the local variable used to capture that arguments value.
numberOfRowsInSection is the name of the parameter that you use when calling this function.
section is the name of the local variable that holds the value that got passed.
So you would call this function like so:
Int rowCount = tableView(tableView: aTabelView, numberOfRowsInSection: 10);
And in that function, you would use the argument like so:
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return section;
}
numberOfRowsInSection is the argument name when calling the function. section is the argument name from within the function. So, for example, you would call this function as follows:
myObject.tableView(aView, numberOfRowsInSection:4)
but from within the function, you would refer to that 4 as follows:
let valueOfSectionArgument = section