I have imported my .h file into a 2nd one, but in the 2nd one i'm trying to do:
FirstClass *firstClass = [FirstClass alloc] init];
[firstClass iconWithType:test];
To match this:
-(void)iconWithType:(NSString *)iconType
But it's not listing iconWithType as a suggestion and i get a warning saying it might not respond to that.
How can i get this to work properly?
My FirstClass is a UIView.
In your FirstClass.h file do you have the method definition in the interface?
I.e.
#interface FirstClass : NSObject {
}
- (void)iconWithType:(NSString *)iconType;
#end
Additionally, the name of the method implies something should be returned. However, it is marked as void.
I'm guessing you just have a return type mismatch. Take a look: does -iconWithType: actually return void? or does it return a UIImage or something else besides?
Related
Hei,
I am using different subclasses for the different view in my application. for that I am now trying to pass a C4Image from one function to another.
My code is as follows:
in TakePhoto.m
cropPhoto= [CropPhoto new];
cropPhoto.canvas.frame=CGRectMake(0, 0, self.canvas.width, self.canvas.height);
cropPhoto.canvas.userInteractionEnabled = YES;
[cropPhoto setup:img];
cropPhoto.mainCanvas=self.canvas;
[self.canvas addSubview:cropPhoto.canvas];
img is declared as a C4Image in the TakePhoto.h
in CropPhoto.m I declared the setup function like this
-(void) setup:(C4Image)image{
//some code here
}
In the TakePhoto.m I'm getting the error
"No visible #interface for 'CropPhoto' declares the selector 'setup'."
I'm doing pretty much the same thing within the one subclass passing NSUIntegers and there it works. So is there anything else I need to do for C4Images or because I'm passing values between subclasses?
Your -(void)setup:(C4Image *)image; needs to be declared n the .h file of the class, otherwise no other objects will be able to call it.
You .h should look something like:
#interface CropPhoto
-(void)setup:(C4Image *)image;
#end
The reason for this is that setup:(C4image*)... is a custom method that you are implementing on your own, so you need to make it visible.
There could be a lot of other reasons as well.* Check out the following answers for different scenarios that generate the same issue:
https://stackoverflow.com/a/10387710/1218605
https://stackoverflow.com/a/14155178/1218605
https://stackoverflow.com/a/13001909/1218605
I must be missing something simple here. Anyway, I started out by just making a regular function,
NSDecimalNumber* aa(NSMutableString *string)
{code}
which I would then call by pressing a button like so:
- (IBAction)parse:(id)sender {
string1=[NSMutableString stringWithFormat:#"%#", screen.text];
NSDecimalNumber *output=aa(string1);}
(screen.text is from a label) However, partway into it, I realized that the function can't use variables from the rest of my viewcontroller.m class (and vice-versa), so I decided to implement the function as a method instead. Here's what I did. First, I added this to viewcontroller.h,
+ (NSDecimalNumber*) aa:(NSMutableString*) string;
#property (nonatomic, strong) NSDecimalNumber *number; //the number I'm working with
synthesized my property, changed my function declaration to this,
+ (NSDecimalNumber*) aa:(NSMutableString*) string
and attempted to call it like this,
NSDecimalNumber *output=[[NSDecimalNumber alloc] aa:string1];
With that attempt, I got two errors -- "No visible #interface for 'NSDecimalNumber' declares the selector 'aa,'" and "instance variable 'number' accessed in class method."
So I tried again with an instance method. Changed the +'s to -'s and instead called the method with
NSDecimalNumber *output;
[output aa:string1];
That corrected the second error but not the first one. I can't figure out why it isn't recognizing the method in the #interface. Also, those weren't the only things I've tried changing -- I've been playing around with multiple ways to call the method, but nothing seems to work. Any ideas?
This function call:
NSDecimalNumber *output=[[NSDecimalNumber alloc] aa:string1];
..is attempting to call aa an instance of NSDecimalNumber. I don't think that's what you want, isn't your aa method a member of your class? Also, you're not calling a class initializer (although you don't need to, since your method is static so long as its definition starts with +):
// MyClass method definition
+ (NSDecimalNumber*) aa:(NSMutableString*) string
// Called with
NSDecimalNumber *output=[MyClass aa:string1];
--UPDATE--
To address the "instance variable" error, you need to make the method an instance method. Change + in definition to - and call it thusly:
// MyClass method definition
- (NSDecimalNumber*) aa:(NSMutableString*) string
// Call it like this _if calling from within MyClass only_ (hence, "self")
NSDecimalNumber *output = [self aa:string];
If you want to add methods to NSDecimalNumber, you need to use a category. Your code adds a method to your view controller subclass.
I've got a simple question.
In Objective-C, when you have a method you want to call, with a return type of void, how you you call it from another method?
The way I've been doing it in my application is this:
[self nameOfMethod];
But that causes Xcode to spit out the following error:
Method '-nameOfMethod' not found (return type defaults to 'id')
Though it seems to still be executing.
Am I calling it right, or is there a better way?
Thanks!
I’m guessing you haven’t declared -nameOfMethod in the class interface and you’re calling it from another method whose implementation precedes the implementation of -nameOfMethod, i.e.:
- (void)someMethod {
[self nameOfMethod];
}
- (void)nameOfMethod {
// …
}
When the compiler is parsing -someMethod and -nameOfMethod hasn’t been declared in the class interface, it generates a warning because it doesn’t know about -nameOfMethod yet.
There are essentially two solutions for this. You could reorder the implementation file so that -nameOfMethod appears before -someMethod, but that’s not always possible. A better solution is to declare -nameOfMethod in the class interface. If -nameOfMethod is supposed to be called by clients of your class, place it in the corresponding header file. On the other hand, if -nameOfMethod is only supposed to be called inside your implementation file, use a class extension. Supposing your class is named SomeClass, this is what your header and implementation files would look like:
// SomeClass.h
#interface SomeClass : NSObject {
// … instance variables
}
// … external methods
- (void)someMethod;
#end
// SomeClass.m
#import "SomeClass.h"
#interface SomeClass () // this is a class extension
// … internal methods
- (void)nameOfMethod;
#end
#implementation SomeClass
- (void)someMethod {
[self nameOfMethod];
}
- (void)nameOfMethod {
// …
}
#end
Using class extensions, the order of method implementations won’t matter.
You need to make sure that your interface file contains a definition for nameOfMethod - so;
-(void) nameOfMethod;
You're calling it correctly, but make sure that the interface for your (void) method is in your .h file.
This is the code
Dest.h
#import <UIKit/UIKit.h>
#import <CoreGraphics/CGPDFArray.h>
#class Model;
// snip
#interface Dest : NSObject
{
CGPDFArrayRef destArray;
DestKind kind;
}
+ (id)destWithObject:(CGPDFObjectRef)obj inModel:(Model*)model;
- (id)initWithArray:(CGPDFArrayRef)array;
Dest.m
#implementation Dest
+ (id)destWithObject:(CGPDFObjectRef)obj inModel:(PDFModel*)model
{
CGPDFArrayRef array = NULL;
Dest* dest = nil;
// stuff to create array
if (array)
{
dest = [[[Dest alloc] initWithArray:array] autorelease];
<path>/Dest.m:63: warning: passing argument 1 of 'initWithArray:' from incompatible pointer type
}
return dest;
}
Clearly the compiler thinks that array is incompatible with initWithArray: declared in Dest.h. But as far as I can see, the type is exactly right. I even copied the declaration from Dest.h and pasted it in Dest.m. initWithArray: compiles fine. Adding/removing the CGPDFArray.h header file in Dest.h doesn't make any difference, the compiler doesn't think it is an int in Dest.h.
I have a feeling you're leaving out another warning that's relevant — "warning: multiple methods named 'initWithArray:' found". If I'm right, this is what you're running into:
There are two method signatures that go with that selector. NSArray's takes an NSArray* and yours takes a CGPDFArrayRef.
alloc returns id. This means that the compiler has no idea what class it returns (yes, the compiler is that thick).
You then send initWithArray: to this mystery object. The compiler says, "Gosh, I don't know what kind of object this is, so I can't decide which method signature is correct. I'll spin around really fast and whichever one I'm facing is the one I'll pick." It chooses NSArray's signature. Then it looks at the argument and says, "Hey, that's not an NSArray! Error!"
The quick-and-easy solution is to change it to [[(Dest*)[Dest alloc] initWithArray:array] autorelease]. The better solution is to choose a distinct selector for your method.
Oh don't do that. Only CFArrayRefs are 'toll-free bridged' to NSArray. The CGPDFArrayRef however is completely different and incompatible. You can not use those as NSArrays.
The PDF API sure looks like a standard Core Foundation compatible one, but it really is not.
From Apple's documentation,
CGPDFArray header file defines an
opaque type that encapsulates a PDF
array
so it cannot be used as a NSArray.
I've got a function called updateTheValue() that I have called using [self updateTheValue] for a while now. Two things happened recently; I added the method calling in the viewDidLoad() method and it spit out a warning saying my class may not respond to this. Second, I want to pass objects to updateTheValue() like strings, but mostly ints, so I declared an NSObject to pass into the method. Can an int fit into an NSObject slot, or what should I use instead?
I would have posted these seperately but they seem to be related since after updating updateTheValue() to accept an NSObject every reference to this function turns out the error that my class "May not respond to -updateTheValue"
You could make your method like this:
-(void)updateTheValue:(NSObject *)anObject
// or use -(void)updateTheValue:(id)anObject
{
if ([anObject isKindOfClass:[NSString class]]) {
// Do your string handling here
}
else if ([anObject isKindOfClass:[NSNumber class]]) {
// Do your number handling here
}
}
Use it like this:
[self updateTheValue:[NSNumber numberWithInt:42]];
I'd suggest doing two different methods though, i.e. updateTheValueWithInt: and updateTheValueWithString: making it easier to read and understand.
Make sure you make the method signature visible before using them, so that the compiler knows what this does.
If you use separate methods you can use int directly without wrapping them into NSNumber objects.
First problem:
updateTheValue() must be declared before you try to call it.
You can either move the definition of function before the calls to it, or add a prototype at the top - eg, add:
(void) updateTheValue;
near the top.
Second problem:
Use an NSNumber, eg [NSNumber numberWithInt:45];