Objective C: Class Extensions and Protocol Conformation Warnings - iphone

I have a large class, which I have divided into several different class extension files for readability.
#protocol MyProtocol
#required
-(void)required;
#end
#interface MyClass : NSObject <MyProtocol>
#end
#interface MyClass (RequiredExtension)
-(void)required;
#end
Is there a better way to do this, without the compiler warning?
warning: class 'MyClass' does not fully implement the 'MyProtocol' protocol

Use a category for each protocol implementation. I use this when I have complex viewControllers.
For example, I have a category that implements NSTextDelegate protocol.
So, MyComplexViewController+NSTextDelegate.h:
#import "MyComplexViewController.h"
#interface MyComplexViewController (NSTextDelegate) <NSTextDelegate>
#end
and MyComplexViewController+NSTextDelegate.m:
#import "MyComplexViewController+NSTextDelegate.h"
#implementation MyComplexViewController (NSTextDelegate)
- (BOOL)textShouldBeginEditing:(NSText *)textObject{
...
}
- (BOOL)textShouldEndEditing:(NSText *)textObject{
...
}
- (void)textDidBeginEditing:(NSNotification *)notification{
...
}
- (void)textDidEndEditing:(NSNotification *)notification{
...
}
- (void)textDidChange:(NSNotification *)notification{
....
}
#end
Then I take all the headers for the main class definition and the categories and combine them into one header which I then import where I need to use the class.

#interface MyClass : NSObject
#end
#interface MyClass (RequiredExtension) <MyProtocol>
-(void)required;
#end
Adopt the protocol in the category.

You don't need to change your style of coding. To get around the warning, it only need to implement "required" method of the protocol, not "optional"

If that's only for readability, you should use categories only. A protocol is not needed in such a case.

Related

Delegate Help - Using class the delegate is for inside the delegate

How would I use the class that the delegate is for inside of the protocol methods.
Ex:
#protocol ILMIconDelegate <NSObject>
- (void)deleteIcon:(ILMIcon *)icon;
#end
#interface ILMIcon : UIView <IconPopoverViewControllerDelegate>
...
#end
This doesn't work because I can't use (ILMIcon *) inside the protocol as it's declared later in the file.
Any help?
Is there any work around, or should I just use (UIView *) instead?
Thanks
Edit: newacct gave me the answer of using #class ILMIcon; before the protocol and it works!
Thanks alot man!
You can forward-declare the class before the protocol declaration, like:
#class ILMIcon;

How to put more than one subclass in objective c

How can I do something like this ?
#interface SomeClass:NSViewController **:NSTableViewController** #end
How can i put two subclases in my class ??
Objective-C does not support Multiple Inheritance.
Typically, you work around this by using protocols when you want to program to an interface.
#interface SomeClass : NSViewController < SomeProtocol >
#end
Another option is composition:
#interface SomeClass : NSObject
{
#private
NSViewController * viewController;
NSTableViewController * tableViewController;
}
#end

How to make a forward declaration for private method?

I'm arranging my methods into groups using #pragma mark in implementation. But sometimes, the method implementation code appears below the code that calls this method, and I'm getting "Instance method not found" warnings. It happens when I'm using private methods. How to fix that?
Simplest method is to use a anonymous category. Add something like this to the top of your .m file, before your #implementation:
#interface MyClass()
- (void)myPrivateMethod;
#end
In your Class.m implementation file, you can add an interface section at the beginning and declare private functions in there:
#interface YourClassName (private)
-(void)aPrivateMethod:(NSString*)aParameter;
...
#end
#implementation YourClassName
...
#end
In this case, you would use a class extension inside of your implementation file to define these methods. In this manner, your 'public' API is still defined in your header file, and your implementation file contains the definition of your pseudo-private methods.
YourClass.m
#interface MyClass()
- (void)myPrivateMethod;
#end
#implementation MyClass
- (void)myPublicMethod
{
// This will not throw an error or warning
[self myPrivateMethod];
}
- (void)myPrivateMethod
{
// Do something
}
#end

Adding category method to NSObject, but getting warnings because it's not in the <NSObject> protocol when I call it

(I found some questions discussing the idea, but I didn't see a solution for my problem.)
I added this convenience method as a category to NSObject. (I've added other methods, so I'm still interested in an answer even if you disagree with this particular convenience method.)
#implementation NSObject (MyCategory)
- (void)performInvocationOnMainThread:(NSInvocation *)invocation waitUntilDone:(BOOL)waitForMainThread;
#end
Then I have a protocol I defined:
#protocol MyDelegateProtocol <NSObject>
- (void)myDelegateProtocolMethod;
#end
Then I declare the delegate as a property of my class that implements said protocol.
#property (nonatomic, assign) id <MyDelegateProtocol> delegate;
But when I try to call the NSObject method I added in my category like so
NSInvocation *invocation = [self.delegate invocationForSelector:#selector(someSelector:withArg:)];
I get this warning
'-performInvocationOnMainThread:waitUntilDone:' not found in protocol(s)
If I cast my delegate as (NSObject *) then I don't get the warning. What am I doing wrong? It didn't seem like I could (or should?) add methods to an existing protocol without creating a "sub protocol" and using it from then on. (Which kind of defeats the point of adding methods to NSObject in mind.)
NSInvocation *invocation = [(NSObject *)self.delegate invocationForSelector:#selector(someSelector:withArg:)];
Your category extends the NSObject class, not the NSObject protocol. While the class now has the method, it's not defined as part of the protocol, hence the warning.
That's why typecasting to the NSObject * pointer type works; you're casting to the NSObject class type, rather than something like id<NSObject> which means an arbitrary Objective-C object that conforms to the NSObject protocol.
You'll have to make an intermediate protocol (or "sub protocol") that extends the NSObject protocol:
#protocol ExtendedNSObject <NSObject>
- (void)performInvocationOnMainThread:(NSInvocation *)invocation waitUntilDone:(BOOL)waitForMainThread;
#end
Then have your delegate protocol extend that one instead:
#protocol MyDelegateProtocol <ExtendedNSObject>
- (void)myDelegateProtocolMethod;
#end
If I'm not wrong, you can keep the existing NSObject (MyCategory) implementation, and they'll play nice together.
when pass/expect this type, qualify it like so:
- (void)race:(NSObject<MyDelegateProtocol>*)arg;

How to create a protocol with methods that are optional?

I noticed methods marked optional in several protocols defined in the iPhone SDK, such as the UIActionSheetDelegate protocol for example.
How can I define a protocol of my own, and set a few of the methods as optional?
From the Apple page on "Formal Protocols":
Optional Protocol
methods can be marked as optional
using the #optional keyword.
Corresponding to the #optional modal
keyword, there is a #required keyword
to formally denote the semantics of
the default behavior. You can use
#optional and #required to partition
your protocol into sections as you see
fit. If you do not specify any
keyword, the default is #required.
#protocol MyProtocol
- (void)requiredMethod;
#optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
#required
- (void)anotherRequiredMethod;
#end
If a method in a protocol is marked as optional, you must check whether an object implements that method before attempting to call it.
As an example, the pie chart view might test for the segment title method like this:
NSString *thisSegmentTitle;
if ([self.dataSource respondsToSelector:#selector(titleForSegmentAtIndex:)]) {
thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];
}
The respondsToSelector: method uses a selector, which refers to the identifier for a method after compilation. You can provide the correct identifier by using the #selector() directive and specifying the name of the method.
If the data source in this example implements the method, the title is used; otherwise, the title remains nil.
Protocols is set of rules. We can create protocols as below example:
TestProtocols.h
#protocol TestProtocols <NSObject>
#optional
-(void)testMethodOptional;
#required // by default
-(void)testMethodRequired;
#end
Implementation:
TestClass.h
#import "TestProtocols.h"
#interface TestClass : NSObject <TestProtocols>
#end
TestClass.m
#import "TestClass.h"
#implemenation TestClass
//optional to implement
-(void)testMethodOptional{
// Your Code
}
//required to implement
-(void)testMethodRequired{
// Your Code
}
#end
Use the #optional keyword before your method declaration to make it optional. Simple as that!
// myProtocol.h
#protocol myProtocol
- (void)myMandatoryMethod:(id)someArgument;
#optional
- (void)myOptionalMethod:(id)someArgument;
#end
// myClass.m
#interface myClass : someSuperClass <myProtocol>
//...
#end
Protocols act the same as abstract classes, so the #optional keyword defines those methods that are optional for implementation.
So, in the code, someMethod1, someMethod2 and someMethod4 are required methods (must be implemented). someMethod3 is optional - if we didn't implement this method, the compiler will not throw any warnings.
#protocol myPrtocol<NSObject>
-(void)someMethod1:(id)someArgument;
-(void)someMethod2:(id)someArugument;
#optional
-(void)someMethod3:(id)someArgument;
#required //by default
-(void)someMethod4:(id)someArgument;
#end
// sampleClass.m
#interface sampleClass : someSuperClass <myProtocol>
//...
#end