Xcode 8 says "Do you want to add a stub?" How do I answer? - xcode8

In Xcode 8, if you declare that a class (or struct) conforms to a protocol, but haven't implemented the required methods (functions?) Xcode will give this message "Type 'ClassName' does not conform to protocol 'ProtocolName'" which is true.
If you open the error by clicking the disclosure triangle, you will see a number of items with grey exclamation mark icons. Each item says "Protocol requires function ... do you want to add a stub?" I do want to add a stub! How do I tell it to add a stub for me?

The answer of raphh is right but xcode is still a little buggy here (at least for me),
If you don't select the Fix-it right after compiling the dot mark transform to exclamation mark and you can't make it appear again unless you try to build one more time.
Right after building :
Dot error icon
When you don't do the Fix-it right away :
exclamation mark error icon
You have to re-build to get the dot icon again when you have several methods to implement and you did do the Fix-it for the first one.

Simply click on Fix-it and Xcode will add you the stub for that method you need to implement.
Like this. See :
Thank you Xcode 8, finally !

Yes, click 'fix it', xcode will add neccessary methods and variables as mentioned in your protocol, and one more thing, Xcode will add variables with proper read write permission in the class implementing the protocol.
For example: in your protocol if you declare a get,set variable and in your struct/class you are declaring it as 'let' property, then Xcode will throw a error saying "Do you want to add a protocol stub", when you click fix it , it now add 'var' property in protocol abiding class/struct for a get,set property in protocol
protocol VoiceAssistant {
var name: String {get}
var voice: String {get set}
}
struct Siri: VoiceAssistant {
var voice: String //xcode added this, when you click 'fix it' for protocol stub
let name = "Siri"
let voice = "Voice" //added by me, Compilation Error: voice is not settable, but protocol requires it.
}

Related

iOS9 storyboard what is unhandled action (handleNonLaunchSpecificActions)?

I've noticed the following error popping up in the console when running my app on iOS 9 when using a storyboard. I'm using xCode7. Is this something I need to be concerned about?
-[UIApplication _handleNonLaunchSpecificActions:forScene:withTransitionContext:completion:] ** unhandled action -> <FBSSceneSnapshotAction: 0x176bfb20> {
handler = remote;
info = <BSSettings: 0x176a5d90> {
(1) = 5;
};
}
There is nothing wrong with your code. This is a logging message internal to Apple, and you should file a radar about it.
There are two hints that show that this is probably Apple's code:
The underscore leading the method name _handleNonLaunchSpecificActions:forScene:withTransitionContext:completion is a convention indicating that the method is private/internal to the class that it's declared in. (See this comment.)
It's reasonable to guess that the two letter prefix in FBSSceneSnapshotAction is shorthand for FrontBoard, which according to Rene Ritchie in "iOS 9 wish-list: Guest Mode" is part of the whole family of software related to launching apps:
With iOS 8, Apple refactored its system manager, SpringBoard, into several smaller, more focused components. In addition to BackBoard, which was already spun off to handle background tasks, they added Frontboard for foreground tasks. They also added PreBoard to handle the Lock screen under secure, encrypted conditions. [...]
I have no idea what the BS prefix in BSSettings is for, but
BS is shorthand for BackBoard Settings, and an analysis of this log message would indicate that it's not anything you did, and you should file a radar with steps to reproduce the logging message.
If you want to try and grab a stack trace, you can implement the category linked to here. Some would argue that overriding private API is a bad idea, but in this case a temporary injection to grab a stack trace can't be too harmful.
EDIT:
But, we still want to know what this action is. So I put a breakpoint on -[UIApplication _handleNonLaunchSpecificActions:forScene:withTransitionContext:completion] and started printing out register values and found a class called FBSceneImpl which had a whole bunch of information about my application:
We are able to find out which private method is called next (stored in the program counter, instruction pointer, register 15.)
I tried finding the un-handled FBSceneSnapshotAction referenced in the log, but no dice. Then, I subclassed UIApplication, and overrode _handleNonLaunchSpecificActions:forScene:withTransitionContext:completion. Now I was able to get at the action directly, but still, we don't know what it is.
Then, I looked at the FBSceneSnapshotAction again. Turns out it has a superclass called BSAction.
Then I wrote a tool similar to RuntimeBrowser and looked up all of the subclasses of BSAction. It turns out that there's quite a list of them:
The two method names we have (one from the log and one from the program counter on the devices) indicate that these actions are used under the hood for passing actions around the system.
Some actions are probably sent up to the app delegate's callbacks, while others are handled internally.
What's happening here is that there is an action that wasn't handled correctly and the system is noting it. We weren't supposed to see it, apparently.
AFAIK, the info above is related to iOS during snapshot the screen (i suppose for double click home multitask related behaviour).I deeply investigated my application and seems that it does not get any side behaviours. You can safely ignore it, for now.
You can use the following gist simple category to test yourself against the calls to the above function:
I have figured it out, it will happen when you have IBAction method declared in .h or .m file but you have not bind it to any control.
.m example:
- (IBAction)click:(id)sender{
}
but not assigned this method to any control in storyboard.
haven't find out why it happens in my app, but at least you can catch the exception, if you want to keep this from popping up in your log pane. It's not a solution, but it might give you more insight why it is happing by inspecting any of the arguments that are passed in the catch.
swift 2 version:
import UIKit
extension UIApplication {
func _handleNonLaunchSpecificActions(arg1: AnyObject, forScene arg2: AnyObject, withTransitionContext arg3: AnyObject, completion completionHandler: () -> Void) {
//whatever you want to do in this catch
print("handleNonLaunchSpecificActions catched")
}
}

Why does Xcode insist on having (id) as method parameter?

My method declaration in the interface:
-(void)doSomethingOnGrass:(Grass*)grass;
While writing the implementation, Xcode's autocomplete feature tends to put this:
-(void)doSomethingOnGrass:(id)grass {
}
Xcode changed the parameter type to id.I realize that I can get it working with id anyway, but I'm not really sure what's the point of changing the parameter type in this scenario. What is it?
Xcode 4.3.2.

How to fix "Protocol Not Implemented"

When I make a new controller called TestController, I insert
<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UISearchBarDelegate>
to the interface of Test.h
When I run the app on the simulator, there are many issues that say
"incomplete implementation" and "method in protocol not implemented"
I know some method is not implemented, but which one? How do I know what method I should add?
Show the issues navigator and expand one of the "method in protocol not implemented" issues. The first detail item will show you where the method is defined in the headers (and, thus, which #protocol it's in). Even better, though, the second detail will show you the name of the protocol.
Switch to the Issues navigator. Ignore the Incomplete implementation message, and look at the Method in protocol not implemented message. You can expand this message to get more information that the compiler provided. The first detail is what you're looking for:
You can see here that I've forgotten to include a method for tableView:numberOfRowsInSection:. I can copy this declaration and paste it into my own code to start writing the method.
Note that there are three issues in my issue navigator. The third is another missing method, which can also be expanded and copied in the same way.
There are two things I can think of which you can look for. First, look in your .h file and see if you have any method prototypes that you may have forgotten to implement, or else implemented and then changed the implementation such that it no longer matched the prototype's method signature.
Second, if the compiler isn't telling you which methods are missing, look at the documentation of UITableViewDelegate, UITableViewDataSource, etc. In the Tasks section, where it lists the various public class and instance methods, it will say "required method" next to any that need to be there. For example, if you class conforms to the UITableViewDataSource delegate protocol, -tableView:cellForRowAtIndexPath: and -tableView:numberOfRowsInSection: are required.
Check your projectname-Prefix.pch file. I got carried away there and imported something I shouldn't have, and that made it look like the one protocol I was implementing in that class was not implemented, when in fact it was. Just go ahead and make sure you aren't including any protocols there.
In my case it was because the file I shouldn't have imported was another protocol file, that was not directly related to the class where I was getting the warning.
Also when you clean out your *-Prefix.pch file, make sure you close and re-open Xcode. I had to do that to get it to take.
Hope this helps!!
Check your implementation file to see if you have included the there. If you have that bit, you need to delete it.

xcode - warning there's no getter/setter for property not even mentioned in the code!

I got the warning :
property 'textField' requires method
'-textField' to be defined - use
#synthesize, #dynamic or provide a
method implementation.
Now, there is no such property defined in my project! More bizarre, if I just click save in Interface builder and build again, the build is successful - though, right on the line with '#end' the warning appears. Also weird: if I begin to write some code ..and then delete it just the way it was before writing it (maybe not code..anything) and then build&go the warning with the textField appears again. Could be a bug of sdk? What could be happening?
Go into interface builder, and click File > Read all class files. Save and quit IB. Go back into Xcode and click Build > Build & Clean. Build your project.

Is there a key combination in Xcode to implement a Protocol?

In Visual Studio if I define a class to implement an interface e.g.
class MyObject : ISerializable {}
I am able to right click on ISerializable, select "Implement Interface" from the context menu and see the appropriate methods appear in my class definition.
class MyObject : ISerializable {
#region ISerializable Members
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
throw new NotImplementedException();
}
#endregion
}
Is there anything anything like this functionality available in Xcode on the Mac? I would like to be able to automatically implement Protocols in this way. Maybe with the optional methods generated but commented out.
XCode currently does not support that kind of automation. But: an easy way to get your code bootstrapped with a protocol is to option-click the protocol name in your class declaration
#interface FooAppDelegate : NSObject <NSApplicationDelegate,
NSTableViewDelegate> {
to quickly open the .h file defining the protocol. From there, copy and paste the methods you're interested in. Those headers tend to be well-commented, which helps in determining which methods you can safely ignore.
I have not seen that feature in Xcode.
But it seems like someone could write a new user script called "Place Implementor Defs on Clipboard" that sits inside of Scripts > Code.
You did not find this useful.
There is not currently such a refactoring in Xcode.
If you'd like it, please file an enhancement request.
I know this thread s a bit old, but I wondered the same thing and found this question.
In my case, I'm defining a property in the interface (.h) and I want to synthesize it in the implementation (.m). I also need to implement methods defined in the interface. Yes, Xcode helps as others have mentioned, but modern IDEs offer these productivity enhancements for things we do frequently. It appears that this is still not a feature in Xcode 4.3.3. However, the feature is available in JetBrains' AppCode. I'm only dabbling with the trial, but it appears to only be possible one property or method at a time, not the whole interface like Visual Studio.
Xcode can help you per protocol method, lets say you have a protocol like this:
#protocol PosterousWebsitesDelegate <NSObject>
- (void)PosterousWebsitesLoadSuccess:(PosterousWebsites*)websites;
#end
in the #implementation section of your .m file you can start writing the name of the method and pressing ESC key to autocomplete the signature of the method/selector:
-(void)Poste (...press ESC...)
Xcode will autocomplete a full signature of the #protocol method, pres TAB to confirm the code.
If you are really committing to learn OSX/iOS Development, I would recommend you to read "XCode 3 Unleashed", a book that really helped me to know Xcode as deep as I know VS :)
check this plugin
https://github.com/music4kid/FastStub-Xcode
it does the thing that you are asking for and more.
Macrumors had a discussion on this too. There is a link to some apple scripts. I haven't actually tried these.