I was tinkering with XCode 4.5.2 this morning and wanting to make a table view I naturally added the UITableViewDataSource and UITableViewDelegate protocols to one on my view controller definitions.
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#end
#import "MyViewController.h"
#implementation MyViewController
#end
Now I know at this stage I need to implement the #required methods of the UITableViewDataSource protocol but out of (presumably TDD) habit I decided to perform a build first with the expectation that the compiler would throw up warnings about the unimplemented #required methods (indicating to me which ones to implement).
But NO the build completed without a single error or warning from the compiler which has confused and concerned me slightly.
So I realize this question is normally the other way round i.e. 'why I am getting this warning', instead of 'why am I NOT getting this warning' but this really simple issue has really baffled me. Clearly I'm doing something wrong but I've no idea what. Any ideas how this might be possible?
FYI this a new project with no build/project setting customisation on a brand new clean install of XCode 4.5.2 on a new Mac Book.
In XCode 4.5.x there is a option for setting different warnings to display (YES/NO). You can check it under build setting Compiler Warnings for LLVM 4.1 compiler.
I hope it'll resolve your issue.
Related
I have this Custom subclass of UIViewController where I've built an UICollectionView.
I am conforming to a CollectionViewDelegates and CollectionViewDataSource in that UIViewController.
Then Xcode asks me to put all the stubs that are needed. There is a lot of stubs. When I worked with Swift, I've added few stubs that I needed like numbersOfItemsInSection or didSelectRowAt. Now I've got over 10. To be completely honest, at this stage I would not even know what is the functionality of most of them. I will figure that out, but my main question is, do I need to use all of them? Or can I just delete them and forget about them right now? Some of them are:
didUpdateFocusInContext
viewWillTransitionToSize
systemLayoutFittingSizeDidChangeForChildContentContainer
etc.
Right click the delegate name in the .h file and jump to the definition. You require all methods not explicitly marked as #optional
I didn't check it for your example but here is a generic one
#protocol MyProtocol
- (void)requiredMethod;
#optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
#required
- (void)anotherRequiredMethod;
#end
I am new to Objective-C, and this compatibility issue of MTKView really confuses me.
I have a view class, and it's used to be like this,
#interface MyView : NSView
#end
#implementation MyView
// A lot of implementations
#end
I am now trying to use Metal to refresh the content in MyView. To use MTKView, which is defined on macOS as
#interface MTKView : NSView
I changed my code to
#interface MyView : MTKView
#end
I compiled my code with the latest SDK so it won't have any issue at compile time.
My questions are,
Since MTKView is only supported on macOS 10.11+, what would happen if I run this code on devices with OS version lower than 10.11? I think it will probably not work since these devices don't know anything about MTKView at runtime. But could Apple had magically considered this issue and let it work on old devices just as NSView at the time my code is compiled?
If it won't work on older OS version devices, what can I do to achieve this without any compatibility issue? What is the common method to solve this kind of compatibility issue? It should work like this but I think runtime inheritance is not a thing.
// Just to show how it should work
if (Is_10_11_Or_Above())
#interface MyView : MTKView
else
#interface MTKView : NSView
I'm trying to learn some of the basics of developing OS X apps with XCode and Objective-C, but I am already running into problems.
I have a project I made from a while back which worked very well for me, however, when I try to replicate the results I had last time, I run into numerous errors.
I have two files, a .c and a .h named "AppDelegate"
in AppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
#interface AppDelegate : NSObject {
IBOutlet WebView *gameFrame;
}
#end
then, in AppDelegate.c:
#import "AppDelegate.h"
#implementation AppDelegate
-(void)awakeFromNib
{
}
#end
In IB, there is an NSObject named 'AppDelegate' and its class is 'AppDelegate'.
However, when I try to run this, I get 11734 errors...
When I click on the error icon at the bottom of the XCode window, it lists a bunch of code that seems to be involving NSStrings, but I cant make any sense of it...
Also, within my code, the
#end
line in both the .c and the .h are highlighted with an error saying:
'Expected identifier or '(' before '#' token.'
I don't understand what XCode is tripping up on when it tries to compile, I don't see any logical place for a '(' to go and I don't think I left anything unidentified.
Any help would be appreciated.
That's because that isn't valid C code.
You named your module file AppDelegate.c, which indicates that it contains source code written in (more or less) pure C. But it does not: You wrote a class interface and implementation in Objective-C, which is a superset of C (all C is valid Objective-C, but not all Objective-C is valid C—in particular, classes aren't).
For this, you must name the module file AppDelegate.m (or anything else, as long as it ends with .m; naming it after the class is a convention worth following). The .m suffix indicates a module (usually containing a class implementation) written in Objective-C.
So, just rename your module file from AppDelegate.c to AppDelegate.m. Make sure you do this in Xcode, not the Finder: If you do it in the Finder, Xcode will only care that there is no longer a file named AppDelegate.c; it won't notice the rename.
For your convenience in creating future classes, Xcode provides a template in the “New File” panel for creating subclasses of certain Cocoa classes; your AppDelegate should be a subclass of NSObject, and templates are also provided for NSView, NSDocument, UIView, UIViewController, and a few others. The files created by the template will already have the correct extensions.
I'm trying to compile some Objective-C code that was written by another programmer on a Mac. I'm using GNUstep for Windows to try and get this code working on my home computer. I really just need his code bits to run, and have little (read: zero) experience with Objective-C. Here is the code:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AntennaPatternGenAppDelegate.m
// AntennaPatternGen
//
//
#import "AntennaPatternGenAppDelegate.h"
#implementation AntennaPatternGenAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
#end
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
and here is the header file:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// AntennaPatternGenAppDelegate.h
// AntennaPatternGen
//
//
#import <Cocoa/Cocoa.h>
#interface AntennaPatternGenAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
#property (assign) IBOutlet NSWindow *window;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I am using the following command to try to compile:
gcc -o AntennaPatternGenAppDelegate AntennaPatternGenAppDelegate.m -I /GNUstep/System/Library/Headers \
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
and getting an error saying "cannot find protocol declaration for 'NSApplicationDelegate'", along with several errors that look like syntax errors (stray '#' in program, and some expected identifiers that aren't there)
I've been googling this problem and it seems like the way that protocol declarations happen in Objective C might have changed at some point? People talk about the difference between 10.6 and previous versions of OSX... I'm not sure if this is the same problem I'm facing, or how to make GNUstep work with this older syntax. Again, I have very little idea what I'm talking about, just beginning to get into this programming, so if I've left crucial information out, please let me know.
Thanks for your time!
I also had same problem. But removing this NSApplicationDelegate from #interface AntennaPatternGenAppDelegate : NSObject NSApplicationDelegate {
it worked fine(compiled with no errors).
I've never used GNUStep, but it looks like they might have renamed the protocol GSAppDelegateProtocol. You could try changing any references to NSApplicationDelegate to that and see what happens.
EDIT:
On second thought, it looks like it might be an informal protocol, so you could probably just take it out of the header file entirely (along with the pointy brackets, remove those too) and everything should work fine.
The short version is that I have a protocol which has an optional parameter. When I build a class that implements it with the iPhone SDK 3.x it compiles just fine, with no errors or warnings. When I used the 2.x SDK I get the following warning:
Class.m:68: warning: property 'field' requires method '-field' to be defined - use #synthesize, #dynamic or provide a method implementation
It works just fine in both cases.
So two questions:
What is the right way to fix the warning? I added #dynamic to the implementation which isn't really correct as the property really isn't there.
Why does work in SDK 3.x but not 2.x? The docs say "On Mac OS X v10.5, protocols may not include optional declared properties." Clearly that's not exactly the case here.
Here's a quick sample of the kind of code I have to make things a little more obvious if I wasn't completely clear.
#protocol MyProtocol
#required
- (void) method:(NSString*)param;
#optional
#property (nonatomic,retain) NSString* field;
#end
#interface MyClass : NSObject<MyProtocol> {
}
- (void) method:(NSString*)param;
#end
The iPhone SDK is not exactly identical to any paricular version of Mac OS X. Clearly a newer version of the toolset is included with SDK 3 that's more similar to the one from Snow Leopard.
The simnple way to remove the warning is to add
#dynamic field;
to your implementation. That tells the compiler that you will provide the implementation dynamically, which you wont, becuase its an optional property, but that should shut the compiler up.
The #optional was introduced in Objective-C 2.0 so it won't be applicable for older versions of the SDK. Your best bet is to determine whether it should be present (probably not) and then #ifdef that around with
#if __OBJC2__
#optional
#property ...
#endif
Then it should only compile under an OBJC2, and it won't be present in the older systems as part of the protocol itself.