How can I get a method to return a Random object of type NSObject - objective-c

I as a learning exercise of Objective C I need to write a method that returns a Random NSObject.The approach that I tried to follow was to trying to get a list of all runtime objects and the itirate through and get an NSObject and return that.
Firstly I am not sure if that is the best approach. Secondly the code I am using is based on this and objc_getClassList link but in Xcode4 I am getting compile time error
warning: Semantic Issue: Implicit declaration of function 'objc_getClassList' is invalid in C99
warning: Semantic Issue: Implicit declaration of function 'classIsSubclassOfClass' is invalid in C99
I have tried adding following headers but no good
#import <objc/runtime.h>
#import <objc/objc-class.h>
#import <objc/objc-runtime.h>
but neither helped me and still have the warning.
So can I please some help with these two things 1- Is this the right approach and what is if not to achieve what I am trying to achieve 2- How can I get this code to not give warnings in Xcode 4.

Well, objc_getClassList is declared in objc/runtime.h, so including or importing that should do it.
For the other part, you simply need to declare your function before you use it. At the top level of the file, or in a file that you then include, you put the function header, without the body:
BOOL classIsSubclassOfClass( const Class aClass, const Class subclass );
This lets the compiler know about the function; you can then #include this declaration in any file that needs to use the function, while defining it (i.e., filling out the body) wherever you like.

So what you want is to get a class, any class, that inherits from NSObject?
I do not think this is the right approach at all. I can not see the need to get any random class. What would you do with it?
Maybe what you want to is add a subset of known classes that are of use to use in an array. And then pick one at random from that array.

Related

Where would I place a global utility function in Objective C?

When writing an iOS app, where would I place a function that I intend to use from any other file?
For example, a function to convert a NSDate to a relative time string ("5 secs ago").
Would I make a class and make these functions all static?
Functions can be placed wherever convenient. If a function or group of functions is likely to be imported in many places, you can declare and implement them in their own .h/.m pair. So for example you might implement your date conversion function in a file named XYZDateUtilities.m, and declare it in XYZDateUtilities.h.
Declaring functions with the static qualifier would limit their scope to the file in which they were declared, so you wouldn't want to do that; in fact you'd want to do the opposite -- declare them as extern in the .h file so that they'll be visible in other files.
You have a couple options:
1) If you're extending the behavior of a class (such as the NSDate string conversion method you described), it may work best to simply create a category on said class.
Here's a tutorial on iOS categories:
http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/
Important Note:
Categories change a class's behavior (if you override a method) everywhere within the project whether or not you include the header (.h) file in another specific class's imports
For this reason, it's generally best to not override methods via a category, but instead, to create a subclass if you want to change certain methods.
For adding new methods, however, categories can be very convenient and useful.
2) If you want to create a new class that's imported everywhere, you can create said class and put its header import, i.e. #import "MyClass.h", into your project's prefix.pch file (found under the "supporting files" group within the project by default).
Anything that you put into the prefix.pch file will be available anywhere within your app. This is also a useful place to put constants (such as strings) or define enums that are used across many classes within the app.
I hope this helps. Let me know if further clarification is needed, and I'll do my best to help.
Cheers!
Another option would be to create a class for your helper methods and implement all the helpers as class methods.
e.g. HelperClass.h
+ (NSString *)getFrenchCapital
e.g. HelperClass.m
+ (NSString *)getFrenchCapital
{
return #"Paris";
}
Then import your helper class wherever you need it, and simply call the class methods:
e.g. Foo.m
#import "HelperClass.h"
...
- (void)logFrenchCapital
{
NSLog(#"Capital of France: %#", [HelperClass getFrenchCapital]);
}
If you make all functions static in a class, then alternative is to just define functions in .m file, and extern functions in .h file, just like what you do in C.

how to make methods in category classes available to other classes?

I want to use a class I got from iosframeworks.com called UIImage+ProportionalFill. I know it's a category extending UIImage, but when I try to use one of its methods in another class I get a message saying no visible #interface for UIImage declares the selector 'nameOfWhateverMethodIWantToUse'. I'm not surprised to get an error, since there must be more to using it than dropping it into XCode, but how do I make the methods in the new category/class available to other classes?
You just need to import your category in the class you like to use it...
#import "UIImage+ProportionalFill.h"
I usually do this in the header file.
The compiler needs to be able to see the declaration of the methods, which should be in the category's header file. You must import the header file wherever you want to use the methods.
You need to #import the header containing the method declaration(s) in each file that uses said methods.
Note tha the methos should be prefixed; i.e. -JDnameOfWhateverMethodIWantToUse.
Note also that adding categories to framework classes willy nilly can easily lead to a rather awfully architected application that becomes difficult to refactor/maintain.
Based on what you said, I think you just forgot to import it.
#import "UIImage+ProportionalFill.h"
Write it on the top of the .h file of the class where you want to use the method.

Where should I define my enumerations?

I have this enumeration:
enum battleType {localEnemy,multiplayerEnemy};
Which is declared in the .h file of my game battle scene. However, I would like to also use those two keywords in my party screen (which is another .h file). Do I have to declare this same enumaration twice (in both .h files), or is there a way to just declare it once and use it everywhere in my project?
As a side note, those keywords are exactly the same as integers, right? So I can't really store them as objects in my array, and instead I have to create a NSNumber instance for this, right?
Declare it in the header file it belongs into. In your case that would probably be BattleScene.h or something like that.
In the other file where you also have to use it you #import BattleScene.h.
If you use typedef, you don't have to write enum all the time. Also, it a good practice to use a uniform prefix in your enums. This makes your code more readable and autocomplete can help you when using the enum.
typedef enum {BattleTypeLocalEnemy, BattleTypeMultiplayerEnemy} BattleType;
There is a way. Create a new .h file (New File -> C and C++ -> Header) and put in your enum. Then just #import it into the battle scene header and the party scene header.
Your other question: Yes, they are really ints, shorts, or some other C integer type.

options to forward declare a C typedef in obj-c #interface

My obj-c class uses a C library (the Chipmunk physics engine), which has an Obj-C wrapper interface.
I want to add a property with a chipmunk type (cpLayers) to my object, like so:
#interface
#property cpLayers layers;
...
The easiest way is to #import "ObjectiveChipmunk.h", but that seems ridiculous to import all of the headers just to get one measly type.
If I #import the C "chipmunk_types.h" file where cpLayers is defined "typedef unsigned int cpLayers;", I get compiler errors related to ARC. They are bridge/casting errors in a macro that is defined in chipmunk_types.h and used in my .m file.
If I add just the definition, or #include chipmunk_types.h, I get redefinition errors.
Is there any better way to do this? And WHY the ARC errors?
Take a look at the ObjectiveChipmunk.h, that is where it overrides the basic Chipmunk types using preprocessor defines. You can add those defines as compiler flags if you want to work around the problem, but I wouldn't really worry about it. You are already doing Objective-C programming after all, have you ever looked at the gargantuan amount of includes that get pulled in when you import something as innocuous as Foundation.h? Importing the full ObjectiveChipmunk.h header is like 1% in comparison.

How do i interface my objc program with an objc++ library?

I have an objc program and i would like to use a widget that is written in objc++ (namely https://launchpad.net/scintilla-cocoa). How do i go about this? Basically i want a new window controller object to interface with this objc++ library to define a scintilla text editor widget. Simply creating a new 'objc class' and accessing the library from there generates a bunch of errors related to the C++ class keyword and so on.
Thanks in advance
Since I'm the one who put you into the (hopefully rewarding :-)) trouble of using Scintilla, here I am.
Let's say we create a ScintillaView subclass, named ppScintillaEditor.
The file should have an .mm extension (e.g. ppScintillaEditor.mm)
The code would be roughly like this...
Interface
#import "Scintilla/ScintillaView.h"
#interface ppScintillaEditor : ScintillaView
{
// your iVars
}
// your properties / methods / whatever
Now, as for the implementation part, remember to put some initialization method to set up the view properly (as in the example accompanying Scintilla-cocoa; I mean the Test project)
Sidenote : Of course, you can create subclasses, categories or whatever on top the ScintillaView class, pretty much based on what you need - I, for example, have create a separate Category just in order to group there some ScintillaView specific commands (sooner or later, you'll notice that for some more advanced Scintilla manipulations, although it's there, it may need some polishing to be a bit more cocoa-friendly, so here you go...)
Now, last but not least...
To resolve the "bunch of errors related to the C++ class keyword and so on", as I've shown in my other video-response to your comment, all you have to do is :
Go to your project's Build Settings
Under Apple LLVM Compiler 3.0 - Preprocessing
Option Preprocessor Macros
Add to both Debug and Release :
SCI_NAMESPACE SCI_LEXER
And that's it. :-)
Hint : The above are defined by Scintilla to avoid clashes between C and non-C elements, like above... so, all it takes is to notify the preprocessor and the rest is taken care of....
you would create an objc class which has the interface your app needs, then implement and add the ivars and implement -- all behind a compilation firewall so the objc++ sources are not included in the header. your implementation would provide any necessary conversions.
it is like you have already done, but you remove the scintilla headers from the header for your wrapper -- they are visible only to your wrapper's implementation.
Update
To illustrate one possible approach:
MONScintillaWrapper.h
// no c++/scintilla sources should be included in this header
#import <Foundation/Foundation.h>
#interface MONScintillaWrapper : NSObject
- (void)setBackgroundColor:(NSColor *)pColor;
#end
MONScintillaWrapper.mm
#import "MONScintillaWrapper.h"
#implementation MONScintillaWrapper
{
scintilla::t_thing scintillaThing;
}
- (void)setBackgroundColor:(NSColor *)pColor
{
...convert pColor to a scintilla color and pass that to scintillaThing...
}
#end