Lefora Free Forum
login join

Recent Posts


3 days, 8 hours ago

Re: call a class from variable, by maddogandnoriko

I do not want to quiet him altogether. Just the dynamic class warning. I suspect it is because the compiler has noway of knowing what class it is going to be so it can not find the method declarations for said class.

Todd

3 days, 22 hours ago

Re: call a class from variable, by clanmills


Hi Todd

Thanks for your thanks.  And for the link.  Interesting.

Yes, Obj/C++ is really good stuff.  It's a software gem.  I've always thought it's kind of difficult to approach and learn.  However I suspect that's just me.  Too much C++ destroys the brain cells.
Anyway, the warnings from the compiler are well intended paranoia.

Several possibilities spring instantly to mind:

1) There's a command-line option to the compiler to say "stop being an old woman"
2) Perhaps there's a pragma to calm his fears
3) You can use a C cast  [(MyClass*) theLetter doThis: data] ;
4) Cast it at birth MyClass* myClass = (MyClass*) [[NSClassFromString ....] ;

I don't know if there is compiler option to silence him.  However I think you should avoid that because those warnings are very helpful for the detecting typos and stuff.  They are really useful.  I suspect option (4)'s the winner.  I look forward to hearing what you find.

4 days, 1 hour ago

Re: call a class from variable, by maddogandnoriko

So I added a class and called it using:

    id theLetter = [[NSClassFromString(chosenLetter) alloc] init];

All is good, and my dynamic class is loaded correctly, and methods from that class can be called. However, I needed to add empty methods to my controller to avoid compiler warnings. Warnings that the methods did not exist. Not really a big deal. But I was browsing the documentation and the recommended way to instantiate it is:

    Class theLetter = [[NSClassFromString(chosenLetter) alloc] init];

But here the empty methods do not passify the compiler.....and I get method missing warnings at compile.

Both of the above lines works correctly, just get compiler warnings.

Any ideas?

Todd


4 days, 22 hours ago

Re: call a class from variable, by maddogandnoriko

Thank you very much for the response. That is exactly what I was looking for. After you told me what I was looking for I did a bit of research myself and found that term. The context in which I found it referenced was for making plug-ins, it all makes sense now! For the record here is a pretty good tutorial of plug-in architecture:
http://www.cocoacast.com/?q=node/175. Thank you for pointing me in the correct direction....I now have a lot more research to do.

Todd

5 days, 19 hours ago

Re: How to use mock and verify methods of OCMock in objective-C ? Is there any good tutorial on OCMock is available on the internet?, by clanmills

Hi Sanjeev

I've had a little google into this matter. There is an on-line tutorial available at:

http://www.mulle-kybernetik.com/software/OCMock/

And I believe you can contact the author of the framework (directly or via a mailing list). 

6 days, 10 hours ago

Re: How to use mock and verify methods of OCMock in objective-C ? Is there any good tutorial on OCMock is available on the internet?, by clanmills

Hi Sanjeev

I don't know anything about this and I'm out of town this weekend (at the Napa Valley Marathon).  If nobody else replies, I'll look at this next week.

6 days, 10 hours ago

Re: call a class from variable, by clanmills

Todd
You can indeed create objects at run time by name with the NSClassFromString method.  Like this:
id xxNumber = [[NSClassFromString(@"NSNumber") alloc] initWithInt:1234];
NSLog("xxNumber = %s\n",[[xxNumber description]UTF8String]) ;
Going home on the train last night I thought "Cocoa must do this".  Although "the book" (Aaron Hillegass's book) talks about storing instances of class objects in the NIB, I don't see how that is possible.  The NIB contains a serialization of an object which is created when the NIB is loaded.  Therefore if the nib loader can create objects by name, we can also.  For that matter I believe you can create classes at run-time.  This very dynamic nature of Obj/C make it much more like JavaScript than C++.  Obj/C++ is C++ and the system therefore provides both the 'compiler centric' C++ object model and a highly dynamic run-time object model like a scripting language.
I wrote the following command-line program to play with this.  RMNumber = RobinMillsNumber (just to make it different from NSNumber = NextStepNumber)
#import <Foundation/Foundation.h>
@interface RMNumber : NSObject
{
int n_ ;
}
- (id) initWithInt : (int) n ;
+ (id) helloWorld ;
@end
@implementation RMNumber
+ (id) helloWorld {
printf("RMNumber +helloWorld() says Hello world\n") ;
return nil ;
}
- (id) initWithInt:(int) n {
n_ = n*2 ;
[super init];
return self ;
}
- (BOOL) respondsToSelector:(SEL)aSelector
{
BOOL result = [super respondsToSelector: aSelector] ;
NSString* methodName = NSStringFromSelector(aSelector) ;
printf("RMNumber::respondsToSelector: %s result = %d\n",[methodName UTF8String],result) ;
return result ;
}
- (NSString*) description {
NSString* me     = [super description] ;
NSString* result = [[NSString alloc]initWithFormat:@"RMString : %@ myValue = %d",me,n_/2] ;
return    result ;
}
@end
int main (int argc, const char * argv[])
{
     NSAutoreleasePool * pool = [[NSAutoreleasePool allocinit];
     // insert code here...
     printf("Hello, World!\n");



[RMNumber helloWorld] ;



if ( argc >= 1 ) {
int n = atoi(argc > 1 ? argv[1] : "911") ;
NSNumber* nsNumber = [[NSNumber alloc]initWithInt:n] ;
RMNumber* rmNumber = [[RMNumber alloc]initWithInt:n] ;
printf("nsNumber = %s\n",[[nsNumber description]UTF8String]) ;
NSLog(@"rmNumber = %@", rmNumber) ;
printf("rmNumber = %s\n",[[rmNumber description]UTF8String]) ;



id xxNumber = [[NSClassFromString(@"NSNumber"allocinitWithInt:n*2];
printf("xxNumber = %s\n",[[xxNumber description]UTF8String]) ;
id yyNumber = [[NSClassFromString(@"RMNumber"allocinitWithInt:n*2];
printf("xxNumber = %s\n",[[yyNumber description]UTF8String]) ;
else {
printf("syntax:   maddog <string> [class]\n") ;
}
[pool drain];
     return 0;
}

6 days, 16 hours ago

How to use mock and verify methods of OCMock in objective-C ? Is there any good tutorial on OCMock is available on the internet?, by sanjeev

My problem is I am getting an error:

OCMckObject[NSNumberFormatter]: expected method was not invoked:setAllowsFloats:YES
 
I have written following Code:

- (void) testReturnStringFromNumber
{
    id mockFormatter = [OCMockObject mockForClass:[NSNumberFormatter class]];
    StringNumber *testObject = [[StringNumber alloc] init];    

    [[mockFormatter expect] setAllowsFloats:YES];
    [testObject returnStringFromNumber:80.23456];
    [mockFormatter verify];
}



@implementation StringNumber

- (NSString *) returnStringFromNumber:(float)num
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setAllowsFloats:YES];
  
    NSString *str= [formatter stringFromNumber:[NSNumber numberWithFloat:num]];
   
    [formatter release];
    return str;
}

@end

1 week ago

Re: call a class from variable, by maddogandnoriko

Hi Robin,

I think you are answering my question. In your example, for example, you are trying to create an object from the 'Monday.m' class, correct? Again in your example, then later in the  code I could call a method  and it would use 'monday.m or tuesday.m or wednesday.m', according to today.. This simple example seems like a harder way of doing it until say, a new day was added to the week, then a new class with the same methods could be created and the existing code should still work on it, like a plug in.

I am sure I did this with PHP and it had something to do with literal, as in using the variable literally. But it escapes me now.

I may be thinking inside my own little box here also. This could very well be a BAD approach to my issue. I am trying to write "good" code and not stray too far from accepted practices. Please let me know if this is the wrong approach.

Thank you,
                   Todd

1 week ago

Re: call a class from variable, by clanmills

Hi Todd

This is an interesting question.  I'm at work in California at the moment (on a Windows machine).  I'll reply in more detail this evening.  The basic answer however is that all objects are derived from a common base class (NSObject).  You can call any method on any object because the run-time system looks up the method before dispatching to theobject.

Remember that Obj/C is C (and Obj/C++ is C++), so you can use a cast to keep the compiler quiet (however you can never fool the run-time system).

NSString* myString = (NSString*) [[NSObject alloc]init] ;  When you call [myString capitalizedString] will fail because the run time system will ask myString if he has a method called 'capitalizedString' and he'll throw when he does not.

If you do:NSObject* notAString = [[NSObject alloc]init] ;
[notAString capitalizedString] ;

The compiler will complain (warn) that he isn't sure that notAString has a method called capitalizedString.

If you want to defer the allocation of objects totally until runtime, something like this:

NSString* today = @'Monday' ; // something you know at run time 
NSObject myObject;
// the following syntax is wrong !!!!
myObject = [[today alloc]init] ; // create an object at run-time

I know I don't have the correct syntax for this - however I'm confident that this is possible.

I hope this sets you off in the right direction.  Please let me know if I'm not answering the question that is in your mind!

1 week ago

call a class from variable, by maddogandnoriko

How can I instantiate a class from a variable contents? For example:
I want to have classes called:
apple.com.h
apple.com.m
mac.com.h
mac.com.m


NSString *x="apple.com";
parseHTML = [[???? alloc] init];


How can I make an instance of apple.com.m without an if x="apple.com" statement? Like below:
parser = [[x alloc] init];

I believe the word is literal. I am trying to leave this 'open' so classes can be added as needed for different domains.

Thank you,
Todd






1 week ago

Re: stringWithContentsOfURL generating compile error, by maddogandnoriko

Whoo! What a simple mistake. Sometimes the answer is right there in front of you, I read through the docs and would have sworn I had it right.


Thank you very much,
                                     Todd

1 week ago

Re: stringWithContentsOfURL generating compile error, by clanmills

I think you've omitted the error: argument in the signature of the API.

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithContentsOfURL:encoding:error:

stringWithContentsOfURL:encoding:error:

Returns a string created by reading data from a given URL interpreted using a given encoding.

+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error

Parameters
url

The URL to read.

enc

The encoding of the data at url.

error

If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, you may pass inNULL.

Return Value

A string created by reading data from URL using the encoding, enc. If the URL can’t be opened or there is an encoding error, returns nil.

Availability
  • Available in Mac OS X v10.4 and later.
See Also
Declared In

NSString.h 

1 week, 1 day ago

stringWithContentsOfURL generating compile error, by maddogandnoriko

Can someone tell me why:
    NSString *content = [NSString stringWithContentsOfURL:theURL encoding:NSASCIIStringEncoding];

is generating compile error:
warning: 'NSString' may not respond to '+stringWithContentsOfURL:encoding:'
(Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)

theURL is of type NSURL, and without the encoding bit, it works....kind of. Returns garbage, which I assume is due to incorrect encoding.

Thanks a bunch,
                            Todd

1 week, 2 days ago

Re: Access to IBM i5 Database (AS/400) from Objective-C., by ludo88

It's what I believe too, there is certainly ODBC support somewhere but i didn't find anything about this.
If someone have informations about it ....
Thx

1 week, 2 days ago

Re: Access to IBM i5 Database (AS/400) from Objective-C., by clanmills

I believe there is ODBC support both on the Mac and in Cocoa.  Obj/C is C  (and Obj/C++ is C++) and the Mac is a UNIX machine.  So if you have existing UNIX code to access the IBM i5, you should be able to build and use that with Cocoa.  However, I personally have never done this and so I can't say anything about the difficulty involved.

1 week, 2 days ago

Access to IBM i5 Database (AS/400) from Objective-C., by ludo88

Hi,

Is it possible to access IBM i5 (AS/400) databases from Objective-C ?

Thx

2 weeks ago

Re: Chapter 12 from Cocoa Programming for Mac OS X 3rd Ed, by clanmills



Hi Raz

Right.  Don't shoot me, I'm only the messenger.  It's the piano keys!  I don't see this in the book - however if you uncheck the 'Alternating Rows' in InterfaceBuilder on your TableView - it works!  Table View Attributes (between Highlight and Col. Sizing)  Top/Right of Center in the screenshot:

If this works for you AND you can't see this in Aaron's book either, perhaps you'd like to send him an email.  I'm sure he'll be pleased to hear about this (if he hasn't already).  I've heard folk say Aaron is very friendly and helpful.  Maybe the piano keys arrived with 3.2 or something (I don't remember).

If you're wondering how I found this, then I used the usual divide and conquer strategy.  I messed about for an hour or so in the debugger, using defaults read and the sample/solution code.  Then I came to the conclusion "there's something in the NIB that's telling him to ignore setBackgroundColor". 2 minutes later it was fixed.  The only fun part of debugging anything is the last two minutes when the pain stops.

2 weeks ago

Re: selecting window to open on CMD+N, by clanmills

Here's a 'no thrills' method of doing it.  It simply inspects the address.

- (IBAction) go : (id) sender 
{
NSString* address = [ addressField stringValue] ;
NSLog(@"%@",address) ;

// sniff for http:// and if missing, update the UI and the address
NSString* http   = @"http://" ;
UInt  http_l = [http length];
bool bHttp = [address length] > http_l ;
if ( bHttp ) bHttp = [[address substringToIndex:http_l] 
 caseInsensitiveCompare: http] == NSOrderedSame;
if (!bHttp ) {
// update the UI
NSString* newAddress = [[NSString alloc]initWithFormat:@"%@%@",http,address] ;
[addressField setStringValue:newAddress];
[newAddress release] ;
// reread address from the control
address = [ addressField stringValue] ;
NSLog(@"rewritten as %@",address) ;
}

// display the address in the webview
    [[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:[NSURL URLWithString: address ]
]];
}

A better way would involve using the NSURL to parse the URL and modify the scheme to http:// if missing or incorrect. An even another better approach would be to get status from the webview to find out if it had failed to load and then try various rewrites to assist the user.  In the original code, the address "file:///Users/bla/dee/bla" may be OK.  However the sniffer above changes an acceptable address into an unacceptable address.
I'm not sure what "no programming is used to make it work" means.  Are you talking about some feature of the IE ActiveX control in Windows?  I believe there's a convention (originally implemented in Mozilla browsers) to modify URLs when they fail to load. 

2 weeks, 2 days ago

Re: selecting window to open on CMD+N, by wolf

it was a little bit hard, but I figured the tutorial out.
one question, how do I make the text say http:// when the user doesn't enter that?
I remember something about that in windows programming, but this one is kinda hard to figure out if you don't know what you're doing, because no programming is used to make it work.

2 weeks, 5 days ago

Re: Chapter 12 from Cocoa Programming for Mac OS X 3rd Ed, by clanmills

Oh, you're still stuck.  That's not so good.  I'm out of town at the Python Conference in Atlanta this weekend and my iMac is at home.  I'll have a look at the zip when I get home on Monday.

2 weeks, 5 days ago

Re: Chapter 12 from Cocoa Programming for Mac OS X 3rd Ed, by raz230

I'm not 'unstuck' as it seems.  I can't see what's wrong here.   I think what I will do is just start over and be more careful.  Thank for the link to the book's code though...

3 weeks, 2 days ago

Re: BDD frameworks for Cocoa/ObjC?, by clanmills

You've lost me!  You're in world that I've never entered.

Of course, another reader might be able to assist, however I'll be surprised because this forum is mostly about people getting started with Cocoa/ObjC on the desktop.  So, while I encourage you to hang around here, I also recommend that you make the same request on a Ruby Forum or a Mac forum such as forums.macosxhints.com

When you find more about this, I hope you'll update this thread.  The Cocoa environment is expanding from the Desktop into both the scripting world (Python and Ruby) and new devices (the iPhone and the soon to be iPad).  I wish you every success in facing these challenges and I'll be delighted to hear about your adventures.

3 weeks, 2 days ago

BDD frameworks for Cocoa/ObjC?, by biot023

Hi -- I'm getting into Cocoa from the Ruby world, and was wondering if anyone is using any BDD technologies with Objective C and Cocoa (& Touch/Cocoa) that they could recommend?
In Ruby, I love the mix of Cucumber and RSpec, and whilst I know these can be used with Cocoa, I'd like to know what else is out there that works best with it.
Cheers,
   Doug.

3 weeks, 3 days ago

Re: setting initial value of popup, by clanmills

Hi M

Take a look at the code for Aaron Hillegass' book.  You can download it from here:   http://www.bignerdranch.com/solutions/Cocoa-3rd.tgz

in 10_Defaults/RaiseMan_B he has code that works with preferences.  I think you're trying to override a preference and I was able to do that in AppController.m (see below - I've added one line to set the background color to Blue).

If you don't have a copy of his book, I strongly encourage you to purchase it.  When I arrived in Cocoa land as a refugee from MFC and Windows, I was really lost and puzzled by the landscape.  I would have starved to death without that book.

I'm not certain that I've really answered your question, however I'm sure you'll be able to use Aaron's code to get to the bottom of this. 


- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
NSLog(@"applicationShouldOpenUntitledFile:");
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:[NSColor blueColor]] forKey:BNRTableBgColorKey];

return [[NSUserDefaults standardUserDefaults] boolForKey:BNREmptyDocKey];
}

3 weeks, 3 days ago

Re: Chapter 12 from Cocoa Programming for Mac OS X 3rd Ed, by raz230

Yeah- that's the reason I bought the book was because  of it's reviews.  Thanks for the link- it's just what I needed smile

3 weeks, 4 days ago

Re: Chapter 12 from Cocoa Programming for Mac OS X 3rd Ed, by clanmills

I'm sorry to hear you are stuck.  That's a really good book and everybody agrees it's the best way to learn Cocoa.  The code/solutions are available from http://www.bignerdranch.com/solutions/Cocoa-3rd.tgz  Build and run the solution.  If the solution works, and your code doesn't - maybe you'll find the fix by inspection.


It does say on p197 "Only windows created after the change will be affected" - so are you sure this isn't user error.  And use the defaults command (page 208) to be sure the color preference is being stored.

Please let me know if you're 'unstuck', otherwise I'll take a look at your zip on Sunday afternoon (I'm in California).   

3 weeks, 4 days ago

setting initial value of popup, by maddogandnoriko

I have a preferences panel that has a popup button that I bound to the Shared User Defaults system. I used the selectedValue and it seems to work, except on the initial launch of the app it is set to nothing. I set up default values on in an init method:

- (id)init
{
    [super init];
    NSMutableDictionary * defaultPrefs = [NSMutableDictionary dictionary];
    [defaultPrefs setObject:@"navDevice" forKey:@"TomTom"];
    prefs = [[NSUserDefaults standardUserDefaults] retain];
    [prefs registerDefaults:defaultPrefs];
    return self;
}


What I want to do now is set the selected item of the popup (navDevicePref) to @"TomTom". Something like:

[navDevicePref ?????:[prefs stringForKey:@"navDevice"]];

 I read through the class description and tried various set... methods and none work in the awake from nib method of my controller.

Todd

3 weeks, 4 days ago

Chapter 12 from Cocoa Programming for Mac OS X 3rd Ed, by raz230

Hello,

This is my first post and I am trying to learn Cocoa.  I'm working my way through this book and I am stuck on a very specific problem.

I am using 10.6.2 and XCode 3.21 which is a little newer than the book I have.

In chapter 12, you work on an application called RaiseMan which is just a simple application that has a list of people and their raises.  It's a Document based application and I have the undo, save and open and basically everything working except one part.

The preferences panel has two options: a checkbox and a colorwell.  The checkbox sets defaults that determines if a new document is opened on startup.  The colorwell sets the background colour of the tableview control on the main document.

The checkbox is working, the colorwell is not.  At least, the color well is setting the color and saving it to the preferences- you can see this when you open the application because the previous color is saved and reloaded.  It's supposes to change the table views background while the app is running.

I can't see what I missed and the program is compiling with no errors.

I've been stuck on this for over a week...

I've uploaded my entire project and wondered if someone would look at it??



Attachment: RaiseMan2.zip (2356.0KB)

3 weeks, 5 days ago

Re: Access preference value, by clanmills

Hi MadDog

Are you saying you're OK - or are you expecting a reply on this one?