I am trying to implement a delegate for an NSWebView, however when I run it, I get this error:
TypeError: Error when calling the metaclass bases
class Delegate does not correctly implement protocol WebScripting: the signature for method isSelectorExcludedFromWebScript: is c#:: instead of Z#::
Where can I find documentation for 'c#::', as opposed to 'Z#::', and what might be wrong with my code?
The method in question is as follows:
def isSelectorExcludedFromWebScript_(self, sel):
return True
Specifically, the NSWebView is documented at: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/index.html (But I suspect that Apple will move this URL in future)
More precisely, the delegate's informal protocol I am attempting to use is documented here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Protocols/WebFrameLoadDelegate_Protocol/index.html#//apple_ref/doc/uid/TP40003828 and https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Protocols/WebScripting_Protocol/index.html#//apple_ref/doc/uid/TP40001562
The only documentation for objc.signature I have found is at: http://pythonhosted.org/pyobjc/api/module-objc.html
The standard objective-c type encodings are documented in Apple's ObjC runtime reference. This defines c as char, # as object, and : as selector, but doesn't mention Z.
PyObjc adds a few that aren't on that list, described in http://pythonhosted.org/pyobjc/api/module-objc.html#objective-c-type-strings by reference to a bunch of constants in the objc module. objc._C_NSBOOL has the value Z (also mentioned in the pyobjc BridgeSupport docs)
So it looks like the problem has to do with the conversion of Python's True to the correct objective c type, but I'm not sure how to correct the problem.
Related
I frequently have relatively complicated protocols with associatedType constraints, are used in generics, are used by CoreData type extensions, etc. I therefore relatively frequently get the error: Type .. does not conform to protocol .... I usually can figure this out after a while, but the error message is really unhelpful -- and usually, if the problem is a minor typo in the signature of a method or something, the bug takes a little while to find. With Java interfaces, IDEs will often report something like method ... not implemented or something, so I know which method to look at in closer detail.
is there a way to make Xcode report details about the members of the protocol which are missing from the conforming type?
When you get that error, click on the arrow to expand it. It will show the required function(s) that are missing.
Here is an example where I implement UITableViewDataSource but I have forgot to provide the "cellForRowAtIndexPath" function.
According to the source of Kotlin's lexer, there is a typealias keyword, and it's not "reserved for future use" like yield and typeof.
Also, the grammar reference suggests that typealias should be a valid keyword for a class member declaration, and when I type typealias in IntelliJ IDEA (Android Studio) with Kotlin plugin it recognizes it as a keyword but I get expecting member declaration error. I have also tried using it with the "usual" syntax, for example like it is implemented in Swift, however with no success.
So, is the typealias feature actually implemented in Kotlin (as of 1.0), and if so, what is the syntax for it? Is there any documentation that describes its use?
Update
Kotlin 1.1 supporting type aliases is now out!
With a typealias, you can provide an alternative name for an existing type since Kotlin 1.1:
typealias Multimap<K, V> = MutableMap<K, MutableList<V>>
For more information, see the official documentation or the KEEP proposal.
The terms "declaration" and "definition" are being used synonymously in Apple's Swift documentation and it's getting me confused.
Under the "Initialization" section (which talks about class initializers), Apple states:
You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition.
Further in a subsection they state:
You can set the initial value of a stored property from within an initializer, as shown above. Alternatively, specify a default property value as part of the property’s declaration.
I thought a variable declaration was different than a variable definition.
You are right that those two mean different thing, THOUGH I think most of the people just use both of them in the same meaning and I think that is also the case of those AppleDocs. Here is great article on subject:
Summary
A declaration provides basic attributes of a symbol: its type and its
name. A definition provides all of the details of that symbol--if it's
a function, what it does; if it's a class, what fields and methods it
has; if it's a variable, where that variable is stored. Often, the
compiler only needs to have a declaration for something in order to
compile a file into an object file, expecting that the linker can find
the definition from another file. If no source file ever defines a
symbol, but it is declared, you will get errors at link time
complaining about undefined symbols.
After doing much searching across the web for legitimate explanations, I have seemed to have found an answer:
The problem is that the two terms overlap to some extent. Definitions also serve as declarations, because they inject an identifier of a certain type to a scope. However, a declaration isn't a definition because it doesn't entail storage allocation for the declared object. To add to the confusion, the semantics of definitions and declarations is slightly different when applied to types and functions, as I will show momentarily. So let's look at a more detailed analysis of these two terms.
Here is the article: Declarations and Definitions.
The article gives further explanation and examples.
Declaration of variable mean to tell compiler their is a var\funct\struct of particular data type. Definition of variable mean asking compiler to allocate memory to variable or define storage for that variable. you can define a variable only one time but you can declare it as many time you want.
I think Apple's Swift 4 Language Reference can be construed as the authoritative answer. From the Declarations section (emphasis mine):
A declaration introduces a new name or construct into your program.
For example, you use declarations to introduce functions and methods,
variables and constants, and to define new, named enumeration,
structure, class, and protocol types. You can also use a declaration
to extend the behavior of an existing named type and to import symbols
into your program that are declared elsewhere.
In Swift, most declarations are also definitions in the sense that
they are implemented or initialized at the same time they are
declared. That said, because protocols don’t implement their members,
most protocol members are declarations only. For convenience and
because the distinction isn’t that important in Swift, the term
declaration covers both declarations and definitions.
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:).
I'd like to get a firmer grasp of how frequently the runtime in any language that requires one is being called. In this case, I'm specifically interested in knowing:
Of all the function calls getting executed on an OS X or iOS system in any given second (approximations are of course necessary) how many of those are Objective-C runtime functions (i.e. functions that are defined by the runtime)?
Of course it depends on your application, but in general the answer is "a whole lot". Like, a whole freaking lot.
If you really want to see numbers, I'd recommend using dtrace to log all runtime functions as they're called. This blog entry talks about how to do such a thing.
A lot. Here are just a few examples.
Every time you send a message, the actual message sending is done by a runtime function (this is in fact the most called runtime function in pretty much any objective C program).
NSObject class and protocol are not part of the standard library but part of the runtime, therefore any method that ends up executing to the default NSObject implementation is in fact executing runtime code.
Every time you execute a default property accessor (either read or write), that's part of the runtime.
If you use ARC, every time you access a weak reference (either for reading or writing it) that's a runtime function.
Objc runtime includes the C runtime, so anything that involves a C runtime function (for example passing a large structure by value or returning it) is in fact calling into the runtime.
and more.