I'm trying to add a method to the CPArrayController class via a category.
The template for adding reverse to the CPString works fine but I'm unable to add anything to the CPArrayController. While compiling I get the error
SyntaxError: * Could not find definition for class
"CPArrayController"
Here is my code:
#import <AppKit/CPArrayController.j>
#implementation CPArrayController (Inserting)
- (CPObject)insertAndReturn
{
if (![self canInsert]) return nil;
var newObject = [self automaticallyPreparesContent] ? [self newObject] : [self _defaultNewObject];
[self addObject:newObject];
return newObject;
}
#end
Any idea why ?
CPArrayController ist Part of AppKit.
So you need to import it, like :
#import <AppKit/CPArrayController.j>
#implementation CPArrayController (Inserting)
- (CPObject)insertAndReturn
{
if (![self canInsert]) return nil;
var newObject = [self automaticallyPreparesContent] ? [self newObject] : [self _defaultNewObject];
[self addObject:newObject];
return newObject;
}
#end
Related
I have a very simple class (currently used as a testing class), which uses delegate/protocol methods to interface with it's parent class. However, I would really like to convert this to use blocks. Yet I can't find a good resource or tutorial out there to help me figure out how to do this. All the blocks tutorials are just way to complicated, and I would really just like a small, concise example of how to do this.
I currently have the class:
#import <Foundation/Foundation.h>
#protocol TestObjectDelegate <NSObject>
#optional
-(void)testObjectSucceeded:(BOOL)passedTest;
-(void)testObjectedFailed:(NSError *)error;
#end
#interface TestObject : NSObject {
id<TestObjectDelegate> _delegate;
}
-(void)compare:(NSString *)stringA with:(NSString *)stringB;
#end
#import "TestObject.h"
#implementation TestObject
- (id)initWithDelegateController:(id<TestObjectDelegate>)delegate {
self = [super init];
if (self) {
_delegate = delegate;
}
return self;
}
-(void)compare:(NSString *)stringA with:(NSString *)stringB {
if ([stringA isEqualToString:stringB]) {
if(_delegate && [_delegate respondsToSelector:#selector(testObjectSucceeded:)]) {
[_delegate testObjectSucceeded:YES];
}
else {
[_delegate testObjectSucceeded:NO];
}
}
else {
if(_delegate && [_delegate respondsToSelector:#selector(testObjectedFailed:)]) {
[_delegate testObjectedFailed:nil];
}
}
}
#end
How could I begin to convert this to a blocks based function? Also, I know 'retain cycles' are something to watch out for when implementing a blocks function. What would I need to watch out for when converting this class to use blocks instead of delegate/protocols? Googling 'retain cycles' also gives some overly complicated answers.
Any starting pointers would be much appreciated?
Maybe this example gives you an idea:
typedef void (^MyCallbackBlock)(BOOL);
#interface TestObject : NSObject {
}
#property (nonatomic, copy) MyCallbackBlock myBlock;
#end
#import "TestObject.h"
#implementation TestObject
-(void) yourMethod
{
...
self.myBlock(YES); // call block with argument
...
}
- (void)dealloc
{
[myBlock release];
myBlock = nil;
[super dealloc];
}
#end
When using the object you can then define the block like this:
TestObject* theTestObject = [[TestObject alloc] init];
theTestObject.myBlock = ^(BOOL theParameter){
NSLog(#"foo");
};
I've Category problem : No visible #interface for 'NSString' declares in selector 'isUrl'
NSString+NSString.h
#import <Foundation/Foundation.h>
#interface NSString (NSString)
- (BOOL)isUrl;
#end
NSString+Nsstring.m
#import "NSString+NSString.h"
#implementation NSString (NSString)
- (BOOL) isUrl {
if ([self hasPrefix:#"http://"]) {
return YES;
} else {
return NO;
}
}
#end
ViewController.m
#import "ViewController.h"
#import "NSString+NSString.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* string1 = #"http://apple.com/";
NSString* string2 = #"Apple";
if ([string1 isURL]) { // **Here is an error**
NSLog (#"string1 is URL");
}
if ([string2 isURL]) { // **And here**
NSLog (#"string2 is NOT URL");
}
}
What I'm doing wrong?
isURL isn't the same as isUrl :)
You've called your method in your category isUrl but you're trying to use a method called isURL in your code.
Though you're better off letting iOS test if something is a URL or not -
-(BOOL)isURL {
return nil != [NSURL URLWithString:self];
}
I am using corelocation framework. is it possible to fake current location for use in social network applications ? if yes then how ?
Please help.
THank you in advance.
:: Edited ::
I made code for that after surfing some more, it may be change using overriding methods of class CLLocationManager.
code like some bellow :
#interface MyHeading : CLHeading
-(CLLocationDirection) newHeading;
-(CLLocationDirection) new1Heading;
#end
#implementation MyHeading
-(CLLocationDirection) newHeading { return 55; }
-(CLLocationDirection) new1Heading { return 56; }
#end
#implementation CLLocationManager (LF)
- (void)setFLocation {
CLLocation *location = [[CLLocation alloc] initWithLatitude:40.778023 longitude:-73.981935];
[[self delegate] locationManager:self didUpdateToLocation:location fromLocation:nil];
id heading = [[MyHeading alloc] init];
[[self delegate] locationManager:self didUpdateHeading: heading];
}
-(void)startUpdatingHeading {
[self performSelector:#selector(setFLocation) withObject:nil afterDelay:0.1];
}
- (void)startUpdatingLocation {
[self performSelector:#selector(setFLocation) withObject:nil afterDelay:0.1];
}
#end
Thank you all for sharing.
Thanks again.
Give the following framework a try.
Anyidea why autocomplete does not work on the spaceScene property?
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#class SpaceScene;
#interface SpaceSceneSingleton : NSObject
{
SpaceScene *spaceScene;
}
#property (assign) SpaceScene *spaceScene;
+(SpaceSceneSingleton*)sharedSpaceSceneSingleton;
-(void) addChildToSceneWith:(CCNode *) node andWithZindex: (int) zIndex;
-(void) runAction:(CCAction*) action;
-(void) setTouchIsEnabled:(BOOL) isEnabled;
-(void) removeChild: (CCNode *) child;
#end
#import "SpaceSceneSingleton.h"
#implementation SpaceSceneSingleton
#synthesize spaceScene;
static SpaceSceneSingleton* _sharedSpaceSceneSingleton = nil;
+(SpaceSceneSingleton*)sharedSpaceSceneSingleton;
{
#synchronized([SpaceSceneSingleton class])
{
if (!_sharedSpaceSceneSingleton)
[[self alloc] init];
return _sharedSpaceSceneSingleton;
}
return nil;
}
+(id)alloc
{
#synchronized([SpaceSceneSingleton class])
{
NSAssert(_sharedSpaceSceneSingleton == nil, #"Attempted to allocate a second instance of a singleton.");
_sharedSpaceSceneSingleton = [super alloc];
return _sharedSpaceSceneSingleton;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
// initialize stuff here
}
return self;
}
-(void) addChildToSceneWith:(CCNode *) node andWithZindex: (int) zIndex
{
[self.spaceScene addChild:node z:zIndex];
}
-(void) runAction:(CCAction*) action
{
//[self.spaceScene add
}
-(void) setTouchIsEnabled:(BOOL) isEnabled
{
}
-(void) removeChild: (CCNode *) child
{
}
#end
You only declared #class SpaceScene; so within this scope nothing more is known than that a class called SpaceScene might exist. Maybe importing SpaceScene.h helps.
I would even say this should compile with warnings. Does it?
I'm making some tabs and I want to have my own delegate for them but when I try to send an action to the delegate nothing happens.
I also tried following this tutorial:
link text
But it doesn't work for me :(
Here is my code:
TiMTabBar.h
#protocol TiMTabBarDelegate;
#interface TiMTabBar : UIView {
id<TiMTabBarDelegate> __delegate;
...
int selectedItem;
...
}
//- (id)init;
- (id)initWithDelegate:(id)aDelegate;
- (void)setSelectedIndex:(int)item;
..
#property (nonatomic) int selectedItem;
#property(assign) id <TiMTabBarDelegate> __delegate;
..
...
#end
#protocol TiMTabBarDelegate<NSObject>
//#optional
- (void)tabBar:(TiMTabBar *)_tabBar didSelectIndex:(int)index;
#end
TiMTabBar.m:
#import "TiMTabBar.h"
...
#interface NSObject (TiMTabBarDelegate)
- (void)tabBar:(TiMTabBar *)_tabBar didSelectIndex:(int)index;
#end
#implementation TiMTabBar
#synthesize selectedItem;
#synthesize __delegate;
...
/*
- (id)init
{
...
return self;
}
*/
- (id)initWithDelegate:(id)aDelegate;
{
//[super init];
__delegate = aDelegate;
return self;
}
- (void)awakeFromNib
{
//[self init];
//[self initWithDelegate:self];
...
}
- (void)setSelectedIndex:(int)item {
selectedItem = item;
if (self.__delegate != NULL && [self.__delegate respondsToSelector:#selector(tabBar:didSelectIndex:)]) {
[__delegate tabBar:self didSelectIndex:selectedItem];
}
...
if (item == 0) {
...
} else if (item == 1) {
...
} else if (item == 2) {
...
} else if (item == 3) {
...
} else if (item == 4) {
...
} else {
...
}
}
/*
- (void)tabBar:(TiMTabBar *)_tabBar didSelectIndex:(int)index;
{
//[delegate tabBar:self didSelectIndex:index];
//if (self.delegate != NULL && [self.delegate respondsToSelector:#selector(tabBar:didSelectIndex:)]) {
//[delegate tabBar:self didSelectIndex:selectedItem];
//}
NSLog(#"tabBarDelegate: %d",index);
}
*/
#end
The delegate only works works inside itself and not in any other files like:
#interface XXXController : UIViewController <TiMTabBarDelegate> {
...
...
IBOutlet TiMTabBar *tabBar;
...
}
...
#end
XXXController.m:
#import "XXXController.h"
#import <QuartzCore/QuartzCore.h>
#implementation XXXController
- (void)viewDidLoad {
[super viewDidLoad];
[self becomeFirstResponder];
...
tabBar = [[TiMTabBar alloc] initWithDelegate:self];
//tabBar.__delegate = self;
...
}
#pragma mark TiMTabBar Stuff
- (void)tabBar:(TiMTabBar *)_tabBar didSelectIndex:(int)index;
{
NSLog(#"Controller/tabBarDelegate: %d",index);
}
#end
None of this seems to work in XXXController. Anyone know how to make this work?
In your XXXController viewDidLoad after you alloc and init a new TiMTabBar are you adding it to the controller's view? If not, perhaps the tab bar in your view that you are clicking on is one loaded from the nib that does not have the delegate set.
It looks like - In awakeFromNib, you are setting the delegate to itself. So whenever you try to send the delegate messages, you are actually sending them to your instance of TiMTabBar, not XXXController.