Argument of '#selector' does not refer to an initializer or method in - ios

I update my project from Swift2.2 to Swift3.0 But "Argument of '#selector' does not refer to an initializer or method" issue received.
Here is code :
for object in Students {
let sectionNumber = collation.section(for: object.firstName!, collationStringSelector: #selector(NSObjectProtocol.self))
sections[sections.count - 1 - sectionNumber].append(object)
}

class Person: NSObject {
#objc var name: String
init(name: String) {
self.name = name
}
}
let p = Person(name: "Alice")
let collation = UILocalizedIndexedCollation.current()
collation.section(for: p, collationStringSelector: #selector(getter: Person.name))
This is also fine since Selector is from Objective-C. Which we need to :NSObject and #objc.

As per docs
func section(for object: Any, collationStringSelector selector: selector) -> Int
Description
Returns an integer identifying the section in which a model object belongs.
The table-view controller should iterate through all model objects for the table view and call this method for each object. If the application provides a Localizable.strings file for the current language preference, the indexed-collation object localizes each string returned by the method identified by selector. It uses this localized name when collating titles. The controller should use the returned integer to identify a local “section” array in which it should insert object.
Parameters
object
A model object of the application that is part of the data model for the table view.
*selector*
A selector that identifies a method returning an identifying string for object that is used in collation. The method should take no arguments and return an NSString object. For example, this could be a name property on the object.
Returns
An integer that identifies the section in which the model object belongs. The numbers returned indicate a sequential ordering.
Solution
Change like below
collation.section(for: "test", collationStringSelector: #selector(getStr)) //here getStr is an other function returning String
func getStr()->String{
return "test"; // this should return an identifying string for object that is used in your collation
}

I implement user2215977 answer but app crash again & again. Now i just change the #selector(NSObjectProtocol.self) to "self". All error gone but just one warning received " Use of string literal for Objective-C selectors is deprecated; use #selector instead ".
If any person have idea to resolve this warning then share me.Otherwise error go now.

Related

Can #dynamicMemberLookup be used to call methods?

In the documentation for #dynamicMemberLookup it says,
Apply this attribute to a class, structure, enumeration, or protocol to enable members to be looked up by name at runtime.
If I'm not mistaken, instance methods are considered members of a struct / class. However, when I try to call a function dynamically I get an error saying:
Dynamic key path member lookup cannot refer to instance method foo()
To reproduce the problem:
struct Person {
var name: String
var age: Int
func greet() {
print("hello, my name is \(name)")
}
}
#dynamicMemberLookup
struct Wrapper {
var value: Person
subscript<T>(dynamicMember keypath: KeyPath<Person, T>) -> T {
value[keyPath: keypath]
}
}
let person = Person(name: "John Doe", age: 21)
let wrapper = Wrapper(value: person)
wrapper.greet() // << Error: Dynamic key path member lookup cannot refer to instance method `greet()`
// Or
let function = wrapper.greet // << Error: Dynamic key path member lookup cannot refer to instance method `greet()`
function()
How can I dynamically call greet() using #dynamicMemberLookup? Is there any way to achieve what I'm trying to do?
Thanks in advance!
No, dynamicMemberLookup does not work for methods. As the signature of the subscript suggests, it only works for things that can be represented as a KeyPath. Method calls cannot be part of a key path. :(
Key-Path Expression
A key-path expression refers to a property or subscript of a type.
The path consists of property names, subscripts, optional-chaining
expressions, and forced unwrapping expressions. Each of these key-path
components can be repeated as many times as needed, in any order.
At compile time, a key-path expression is replaced by an instance of
the KeyPath class.
I suspect the reason why it is called "dynamic member lookup" is because it also works with subscripts. The alternative of dynamicPropertyOrSubscriptLookup is rather a mouthful isn't it?
One rather hacky fix would be to change greet into a computed property:
var greet: () -> Void { {
print("hello, my name is \(name)")
} }
If greet has had parameters, you could also change it into a subscript, but I think that is an even uglier solution.

Can't perform methods of objects stored in Array[Any]

I want to store objects of different types in an array.
The program below is only a minimum demo. In the anyArray:[Any] an instance of Object1 is stored. The print statement prints out the expected object type. In the following line the test of the stored object's type returns true. This means, during run time the correct object type is known and every thing seems to be fine.
class Object1 {
var name = "Object1"
}
var anyArray:[Any] = [Object1()]
print("\(type(of: anyArray[0]))")
let testResult = anyArray[0] is Object1
print("Test result:\(testResult)")
//print("Name:\((anyArray[0]).name)")
Console output:
Object1
Test result:true
However, if I try to print out the name property of the object, I get an error message from the editor:
Value of type 'Any' has no member 'name'
Well, at compile time the object's type is unknown. That's why the compiler complains. How can I tell the compiler that it is OK to access the properties of the stored object?
The difference comes from the difference from Type Checking in:
runtime, or
compile time
The is operator checks at runtime whether the expression can be cast to the specified type. type(of:) checks, at runtime, the exact type, without consideration for subclasses.
anyArray[0].name doesn't compile since the Type Any doesn't have a name property.
If you're sure anyArray[0] is an Object1, you could use the downcast operator as!:
print("\((anyArray[0] as! Object1).name)")
To check at runtime if an element from anyArray could be an Object1 use optional binding, using the conditional casting operator as?:
if let:
if let object = anyArray[0] as? Object1 {
print(object.name)
}
Or use the guard statement, if you want to use that object in the rest of the scope:
guard let object = anyArray[0] as? Object1 else {
fatalError("The first element is not an Object1")
}
print(object.name)
If all objects in your array have a name property, and you don't want to go through all the hoops of optional binding repeatedly, then use a protocol. Your code will look like this:
protocol Named {
var name: String {get set}
}
class Object1: Named {
var name = "Object1"
}
var anyArray:[Named] = [Object1()]
print("\(type(of: anyArray[0]))")
let testResult = anyArray[0] is Object1
print("Test result:\(testResult)")
print("Name:\(anyArray[0].name)")
Notice that anyArray is now an array of Named objects, and that Object1 conforms to the Named protocol.
To learn more about protocols, have a look here.
You object is still of type Any. You just checked if it can be of type Object1, but you did not cast it. If you want the object as Object1, you need to cast it.
Also if multiple classes can have name, you need to use Protocol like #vadian has mentioned in his comment and cast it to that protocol.
protocol NameProtocol {
var name: String {get set}
}
class Object1: NameProtocol {
var name = "Object1"
}
if let testResult = anyArray[0] as? NameProtocol {
print(testResult.name)
}
Edit: "I want to store objects of different types in an array". The solution that you have marked as correct will not work if all the objects that you have do not conform to the protocol.

Pass a reference to an instance variable?

I want a method to set a value for an instance variable but I want to tell it which variable to set the value for each time I call the function.
var first: Key!
var second: Key!
func setKey(for selectedKey: Key) {
let key = // key selected from picker
selectedKey = key
}
func someWhereElse() {
setKey(for: first)
setKey(for: second)
}
Key is a class so it’s a reference type. Am I understanding reference mechanics right or is what I want a reference to the reference? (Some kind of C type pointer)
It should be
func setKey(for reference: inout Key!) {
let key = // key value you want to set
reference = key
}
Then you can invoke the method as following,
setKey(for: &yourKeyValue)
If you want to point the reference to class object's instance member
setKey(for: &(yourObject.yourVariable) )
According to your code, it will be
var first: Key!
var second: Key!
func setKey(for selectedKey: inout Key!) {
let key = // key selected from picker
selectedKey = key
}
func someWhereElse() {
setKey(for: &first)
setKey(for: &second)
}
Note the inout attribute in parameters. This is very much like pointer in C.
In your case, classes are like already pointing to some kind of memory block (* Key).
So, what you need is pointer to pointer (** Key).
This can be done with inout Key or UnsafeMutablePointer<Key>. As Swift doesn't encourage usage pointers, inout Key should be used for safety.
Note that if you want to point to Key!, it should be inout Key!.
If for Key, it should be inout Key.
Key and Key! are not the same.
If you want to work with Unsafe Swift, here is the code
func setKey(for reference: UnsafeMutablePointer<Key!>) {
let key = // key value you want to set
reference.pointee = key
}
then you can call it the same way as inout,
setKey(for: &yourValue)
One solution is make variable identifier in Key class.
Set
first.identifier = 1
second.identifier = 2
Check this value in setKey method to identify the instance variable
Second solution is in setKey method, check reference
if first === selectedKey {
// First Instance
} else if second === selectedKey {
// Second Instance
} else {
print("the two instances are not identical!")
}
If the class that contains the instance variable is not a subclass, you can have it inherit NSObject so that it contains the setValue(_ value: Any?, forKey: String) method. You can then set the value of an instance variable of an instance of said class with a string as the key.
As for your understanding of reference mechanics, you are right that first and second would be passed by reference. However, if you change the value of selectedKey, you are not changing the value of first or second, you are changing the reference. selectedKey holds the reference, so to change the value of the object it references, you must change its properties, for example selectedKey.value = key. Changing the value of selectedKey itself changes the reference, so the object it is referencing will not be changed.

.self after struct type in Swift

I’m confused by a line of code found in the Metal example where the memory pointer is bound to a type.
uniforms = UnsafeMutableRawPointer(uniformBuffer.contents()).bindMemory(to: Uniforms.self, capacity: 1)
My confusion is the .self after the Uniforms type. Uniforms is a struct defined in an Objective-C file and the code wont run without .self being in the call. Why is that necessary?
The .self returns the metatype instance for the corresponding type. Think of it as a typesafe type identifier (e.g., way safer than using a string for that). You can then safely call the available initializers, static methods, static properties on such metatype instance.
For instance, you could store it in a variable as well:
let metatype: Uniforms.Type = Uniforms.self
and Uniforms.Type is the actual metatype (i.e., the type's type).
Metatype crash course. A very quick example to get a feel of how this meta stuff might be actually useful:
class Super {
let id: Int
required init(id: Int) { self.id = id }
}
class SubA: Super { ... }
class SubB: Super { ... }
let subclass: Super.Type = SubA.self
and then, later on, use subclass to create an instance without hardcoding the actual subclass type name:
let obj = subclass.init(id: 123) // new SubA instance.
In Swift, .self could be used on a type to extract its meta type or on an instance of a type. Example, use .self to get the meta type and pass it to the API:
self.tableView.registerClass(
UITableViewCell.self, forCellReuseIdentifier: "myUIViewCell")

Instatiate Realm Object from string in swift 3

I would like to know if it is possible to instantiate a realm object based on a string that is the class name of the realm object but without knowing what that string will be until it is provided.
For example:
for(_, object) in json["AllObjects"]{
let objectType = self.getRealmObjectBasedOnString(type: className, params: object.stringValue)
self.objectList.append(objectType)
}
Here I go through a json that I get and want to create a realm object from each json object in the array. The problem is that this method will be called several times and each time the only thing that will change is the className variable. So I would like to keep this logic in only one method instead of creating several methods with same logic or a huge and complicated if else that determines the realm object to be created.
Here is getRealmObjectBasedOnString
func getRealmObjectBasedOnString(type: String, params: String) -> Object{
switch type {
case "classA":
return ClassA(JSONString: params)!
case "classB":
return ClassB(JSONString: params)!
default:
return DefaultClass(JSONString: params)!
}
}
Can someone explain why this does not work and whether it is possible to accomplish what I want?
You can use NSClassFromString to get Realm object type from string, but keep in mind that Swift uses modules for nemespacing, so you'll need to add your app's module name before your class name.
guard let objectType = NSClassFromString("YourAppModuleName.\(json["className")") else {
// handle unexpected class here
}
let objectList = realm.objects(objectType)

Resources