Instance method xyz not found (return type defaults to id)? - ios

I am facing issue for a method, which I have using for other NSString and working fine.
See this image.
Here html is NSString, I have used it in other project, like a clone of this, its working fine there, even I wrote like this, working fine in that project,
html = [html stringByConvertingHTMLToPlainText];
but here in this method , both the ways giving this warning.
What that mean and how can I solve it
?

If htmlis typed as an id, but you know that it's in fact an NSString, try casting it to NSStringand call your selector from there (id type to NSString).
That said, the reported error often occurs when your method is not declared in your header file (https://stackoverflow.com/a/13154244/865688). Did you forget to #import?

"stringByConvertingHTMLToPlainText" is not a standard method defined in the NSString class. It is s commonly used category defined in the MWFeedParser project.
Make sure you have imported the category using the right syntax. Refer: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html Search for text "import the category header file" read around that area if not the whole chapter.

Related

Xcode use of unresolved Identifier error

I am new to Xcode so I didn't get how to resolve this error by reading other articles in StackOverflow related to this. Basically I am working on the CoreData part and the following code accesses Amount, Bank Name and Title field but it's giving this error.
The first four lines in your code are outside any context. You can't do that. No executable code can appear except inside a function — usually, some method of some class.
So those four lines need to be inside the ViewController class, not before it, and again, not just loose inside it, but in some method of the ViewController class.

iOS Xcode: Compiler error accessing Ivar in Objective-C

I am working in a large existing objective-c codebase, writing unit tests at the moment. The project uses instance variables widely.
I wrote a little method to grab the ivar for something. It worked in another unit test in the same project, but isn't working in this case.
Code:
-(id)getObjectForIvarNamed:(NSString *)ivarNameString
{
const char *ivarName = [ivarNameString UTF8String];
Ivar ivarValue = class_getInstanceVariable([textFieldOverlay class], ivarName);
id objectAtIvar = object_getIvar(textFieldOverlay, ivarValue);
return objectAtIvar;
}
The compiler complains at the line starting Ivar ivarValue with the following error:
"Declaration of 'Ivar' must be imported from module 'ObjectiveC.runtime' before it is required"
A web search for this error code comes up with zero hits. Not sure why it works in another file, the headers all look the same between the two XCUnitTest classes.
Never fails, found the answer as soon as I had typed the question. But, I hadn't posted yet. Because this error was impossible to find in a web search, I figured I should post it, so there is an instance out there. The project's class I was testing in the working test class imports the objective-c runtime header, I thought to check the tested class after I typed the question up. I thought it was a header issue but didn't see it in the imported class. Learning moment.
To import the objective-c runtime header and eradicate this warning, add #import <objc/runtime.h> to the header. Done, and done.

"Header File "Only able to be read in one class and other init methods won't work in other files

I have been stumped on this for awhile. I have asked multiple developers I know and they think I have forgotten to "#import the .h file". But I know I have, I have tested the class in more than one file in my project. It only works in the "VNDecalLevelListViewController.h" ( which I will post its implementation if a picture). When I try and call my "initForNewDecal" method for my "VNDecalCreatorViewController.h" class in my "VNAdminViewController.h" class I received the error that this method has not been declared in "VNDecalCreatorViewController.h". But when I call it in my "VNDecalLevelListViewController.h" class it works.
I am able to allocate and use "init" to create the object and it loads with a work around I made. But I am new to programming and I can tell there is definitely a better solution.
As you will soon see as i got to allocate the VNDecalCreatorViewController in the " VNAdminViewController" the option to initialize VNDecalCreatorViewController with the proper initializer "initForNewDecal" isn't even a option.
Anyone know why this is happening ? I want to write the code right I am trying to figure out why my header file is only being read in one class.
I guess this is because you mutually imported between the two class Creator and Level. I mean you may have #include "VNDecalCreatorViewController.h" in VNDecalLevelListViewController.h and vice versa. The solution is to use #class to forward declare any classes you may need to reference instead of #import'ing the header.
Make sure that the method is declared in the header file and implemented in the .m file.

NSMutableArray and indexAtObject

I am a newbie to Objective-C and I am working on getting a good handle in working with arrays.
According to Apples own documentation NSMutableArray inherits from NSArray. I am seeking to use the method objectAtIndex:i within a for loop. Yet Xcode is claiming that
"Property 'objectAtIndex' not found on object of type
'NSMutableArray'".
Within a for loop I am performing (or seeking to) the following test:
if([self.cards objectAtIndex:i].isChosen){
do something here }
I am certain I not doing this right. It can be frustrating learning the idiosyncrasies of a new programming language. For me Objective C has, so far, borne little resemblance to C++ or C.
Any pointer or assistance would be greatly appreciated.
Thanks in advance
Walt Williams
From your error message, you could not be using the method and parameter correctly. Possibly you're trying to use a dot notation?
Your code should be like:
id object = [array objectAtIndex:index];
where the index comes from your loop and you then use the object.
In addition to Wain's answer, which is correct, but for the sake of completeness, you are using the array and trying to call the method this way:
Ex:
array.objectAtIndex.i = 5; //Java.....?
which is the cause of this error:
"Property 'objectAtIndex' not found on object of type
'NSMutableArray'".
It is complaining that you are trying to access a property named "objectAtIndex", which of course, doesn't exist.
You mean to call the method:
[array objectAtIndex:i];
In Objective-C, it is called "you are sending a message (objectAtIndex:i) to the array".
I am guessing, but hard to tell without your header file declarations, that your problem is you need to #import the file where your class is declared. This is the most common problem when having issues like this. It somehow does not know that it is an NSMutableArray. Also as I noted above you need to assign the value you get back from the array into a typed variable of that class so that you can then access the isChosen property.

How to determine the structure of an iOS (id) object?

When I call a certain function from my iOS app, it returns an id data type. I can't see into this function so I don't know what it's doing or what the return type really is.
If I print it to console using NSLog("#"...) I get a string similar to this:
2012-01-18 19:03:08.915 HelloWorld[165:707] Key Press State.
Is there any way for me to determine the structure of this basic Id object? How would I go about getting a specific part of that response out, such as "Key press state". String parsing seems like a horrible idea, but maybe that's the only way. Perhaps the data really is just an NSString?
Thanks!
Try this:
NSLog(#"Mystery object is a %#", NSStringFromClass([mysteryObject class]));
If you look in <objc/runtime.h> you'll see methods for querying an object about its class, method selectors, ivars, etc. However, you don't usually want to do that, as it breaks encapsulation and can lead to you relying on implementation details that might change in the future, so be careful with it. You can read more here.

Resources