Lefora Free Forum
login join

Recent Posts


1 day, 21 hours ago

Re: Threading/Blocking and avoiding the Beachball, by clanmills




Todd

Can I validate the specification here with you, please?  I've written a little script:

#!/bin/bash

count=0
while read line;
do
count=$(($count+1));
url=http://clanmills.com/${line}
echo url = $url lines = `curl --silent http://clanmills.com/${line} | wc -l`
done < $1
echo "processed : $count files"

which reads the file:

BoydWedding.html
NicholasBirthday.html
arizona.html
dennis.html
eventsevents.html
gps.html
gpsfoo.html
popup.html
rollovermap.html
rollovermap1.html
statesonly.html
utah.html

and produces the output:

543 /home/rmills/temp$ readem.sh readem.txt
url = http://clanmills.com/BoydWedding.html lines = 10
url = http://clanmills.com/NicholasBirthday.html lines = 10
url = http://clanmills.com/arizona.html lines = 14
url = http://clanmills.com/dennis.html lines = 28
url = http://clanmills.com/eventsevents.html lines = 509
url = http://clanmills.com/gps.html lines = 83
url = http://clanmills.com/gpsfoo.html lines = 280
url = http://clanmills.com/popup.html lines = 32
url = http://clanmills.com/rollovermap.html lines = 104
url = http://clanmills.com/rollovermap1.html lines = 104
url = http://clanmills.com/statesonly.html lines = 10
url = http://clanmills.com/utah.html lines = 10
processed : 12 files

So, it's reading the names of files from readem.txt and building URLs which it then gets using curl.  For simplicity, all I'm doing in a simple line count on the file as proof that all the files are different.

I've written a command-line tool version of this in Obj/C++ (code below) and here's the output:

2010-03-14 20:57:28.599 readem[25593:a0f] http://clanmills.com/BoydWedding.html lines = 11
2010-03-14 20:57:28.697 readem[25593:a0f] http://clanmills.com/NicholasBirthday.html lines = 11
2010-03-14 20:57:28.794 readem[25593:a0f] http://clanmills.com/arizona.html lines = 15
2010-03-14 20:57:28.890 readem[25593:a0f] http://clanmills.com/dennis.html lines = 29
2010-03-14 20:57:29.091 readem[25593:a0f] http://clanmills.com/eventsevents.html lines = 510
2010-03-14 20:57:29.191 readem[25593:a0f] http://clanmills.com/gps.html lines = 84
2010-03-14 20:57:29.300 readem[25593:a0f] http://clanmills.com/gpsfoo.html lines = 281
2010-03-14 20:57:29.397 readem[25593:a0f] http://clanmills.com/popup.html lines = 33
2010-03-14 20:57:29.498 readem[25593:a0f] http://clanmills.com/rollovermap.html lines = 105
2010-03-14 20:57:29.601 readem[25593:a0f] http://clanmills.com/rollovermap1.html lines = 105
2010-03-14 20:57:29.697 readem[25593:a0f] http://clanmills.com/statesonly.html lines = 11
2010-03-14 20:57:29.793 readem[25593:a0f] http://clanmills.com/utah.html lines = 11
2010-03-14 20:57:29.984 readem[25593:a0f] http://clanmills.com/  lines = 212
2010-03-14 20:57:29.984 readem[25593:a0f] processed = 13 files

Let's ignore the arithmetic isn't quite correct and the Obj/C++ version seems to count an extra line (and has 13 files instead of 12).  Let's not worry about those details which have to do with a trailing blank line both in readem.txt and the code from pulled from the internet.

Just for grins, I duplicated all the lines in readem.txt (so there were 2600 URLs to be read).  It ran in about 2 seconds.  Kind of amazing, isn't it?  In fact, the console had the comment "*** process 12345 exceeded 500 messages per second limit, remaining messages this second discarded ***".
Obviously we can use this little program below a GUI, with a button to select the input file, and a scrolling view gizzmo to display the output and so on.  And maybe you could double click on an output line and see the file .... and all the other stuff that makes UI programming both fun and never-ending.
Can you confirm that I've understood the task? How long is a "long list?" (100s, 1000s, 1000,000s)? 
I know you're using XCode 2.4 and don't have the garbage collector stuff (which was thrown into my code by the Wizard in XCode 3.2).  However that's a distraction from understanding the mission and the issue at hand.
If you're reading a lot of very long files from the internet, this could take rather a long time and if you're not releasing the objects correctly, you could be eating the computer which will probably cause the beachball to show up.
Robin

Here's the code:


#import <Foundation/Foundation.h>

static int lineCount(NSString* s)
{
NSArray* listItems = [s componentsSeparatedByString:@"\n"];
return  [listItems count] ;
}

int main (int argc, const char * argv[])
{
if ( argc != 2 ) return printf("syntax: readem file") ;

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSStringEncoding encoding ;
NSError* error ;
NSUInteger urlCount = 0 ;

// get the lines from the input file
NSString* fileName = [NSString stringWithUTF8String:argv[1]];
NSString* fileString = [NSString
stringWithContentsOfFile : fileName
usedEncoding : &encoding
error : &error
];
NSArray* lines = [fileString componentsSeparatedByString:@"\n"] ;

// run over the lines, convert them to URLs and read them from clanmills.com
NSUInteger i ;
for ( i = 0 ; i < [lines count ] ; i++ ) {
urlCount ++ ;
NSString* urlString = [NSString stringWithFormat:@"http://clanmills.com/%@",[lines objectAtIndex:i]];
NSURL* url = [NSURL URLWithString:urlString];
  
NSString* s = [NSString 
stringWithContentsOfURL : url 
usedEncoding : &encoding
error : &error
];
NSLog(@"%@ lines = %d",urlString,lineCount(s)) ;
}
NSLog(@"processed = %d files",urlCount) ;
    [pool drain];
    return 0;
}






3 days, 3 hours ago

Re: Threading/Blocking and avoiding the Beachball, by clanmills

Todd

I'll happily look at this for you next week (if I don't go to England).  Can you zip your code and send it to me?  I won't steal your code - honest - I'm only trying to help.

I'm rather busy during the next few days with opensource stuff which I have promised.  Once I'm clear of that, I'll be happy to look at your stuff.

3 days, 3 hours ago

Re: Hillegass 2nd and 3rd Editions. Which one?, by clanmills

Todd

OK.  I'm in California.  I'll mail you the book if you wish.  However I might go to England on family business next week.  

The second edition is such an end-of-life book that it's rather expensive for what it is.  Maybe you can get a second hand copy from Amazon.  I think the 3rd Edition is very good value.  If you get a new machine, spend $50 on the book - it's worth it.

I have to admit to lusting after an 27" iMac.

3 days, 5 hours ago

Threading/Blocking and avoiding the Beachball, by maddogandnoriko

I have been tinkering with the little cocoa I have learned so far. I have put together a simple project that goes through a list of urls one by one, puts the url into the "currently processing" text field, and gets the html from each, and makes sure there is no "error" text(post not found, article doesn't exist, etc.) . That's it for now and it works...kind of. For a short list it seems to work, for a long list it does what it should but the dreaded beachball appears. Also the text field is not getting updated for each url, it is only getting updated after the last url is finished. I have tried a bit of research and found that my threading or blocking is incorrect?????

I am not looking for a specific answer to my problem here, there is also way too much code to post. I am not looking for you guys to code this for me. And all in all it is a throw away project in the end, or at best will need to be recoded. What I am looking for is what do I need to research more to ovoid this problem? Maybe some common errors that cause this?
Here's basically what's going on:

1. button pressed sends 'startDownloading' to appController.
2. startDownloading has a loop inside that does the rest.

I have created a class to house and parse, and return bits of the html.



Todd

3 days, 5 hours ago

Re: Hillegass 2nd and 3rd Editions. Which one?, by maddogandnoriko

Thank you for the offer, but alas, I am in the USA. I will purchase the book online, it will be well worth the money, and someday my poor little iMac will just not be enough and I will purchase a new Mac. But I tend to do with what I have, and will likely continue until I am no more. I suspected 2nd edition was the one I needed. But the preface in the 3rd mentions os x 10.4 and was confused. Thank you for the clarification.

Todd

3 days, 18 hours ago

Re: Code for Saving a Document, by clanmills

Well there are lots of possible answers here.  If you've build a document then writing your objects to file is very straightforward.  I recommend that you get a copy of Aaron Hillegass' book and he deals with this (Chapter 10 - Archiving) And reading/writing preference files (Chapter 15) and all manner of good stuff.

If you've build something more 'vanilla' (not based on the NSDocument class) then remember Obj/C is C (and Obj/C++ is C++) so you have 100% of the platform library at your disposal.  So fopen/fread/fwrite/fclose C library calls are available, and the std STL template classes in C++.

The NSData class provides a "initWithContentsOfFile" and "writeToFile" methods for binary I/O.  And foundation classes such as NSString and NSDictionary have methods to read and write disk files.

Just to give you even more choice (and confuse you totally), you can read/write XML, read/write SQLite databases and even use HTTP posts, gets and puts to push/pull your data over the web.

And I wouldn't be surprised if there were even more choices that I'll remember after I've pressed the "Submit Reply" button. 

3 days, 18 hours ago

Re: Hillegass 2nd and 3rd Editions. Which one?, by clanmills


Hi Todd
I have both editions of the book.  I have a 6 year old PPC Powerbook 17" machine running Tiger and a 24" iMac running Snow Leopard (and it has a Leopard partition).  I keep all three systems available to support my opensource activities.

I bought the second edition of the book about 3 years ago and that's fine for Tiger.  XCode 3 brought many enhancements to Cocoa and in particular a complete rewrite of Interface Builder.  The 3rd edition of Aaron's book is for XCode 3.  All of this arrived with Leopard in the fall (autumn) of 2007.

Obj/C did indeed advance to Obj/C 2.0 with XCode 3.  The "big new thing" in 2.0 is garbage collection.  It also brought the property keyword and some other stuff.  And I believe the Mac's 64bit UI support is only available via cocoa (carbon has not been promoted to 64 bit).  If you want to write code for the iPhone (or iPad), the garbage collector is not available on that platform.

However, I agree however that it is 'basically the same'.  Having the book that matches the software does lower the pain involved in figuring out this stuff.  I think I said before to you that I think it's rather difficult to learn cocoa and I'm not sure I can really explain why - it's just a feeling I have about it.  For sure I've often seen folks write things like "you have no hope of learning cocoa without a good book".  And many believe Aaron's book is the best available.  I highly encourage you to have the edition for your version of XCode.

My 2nd edition sleeps in my book case and I'd be happy to lend it to you if there's an easy way to send it to you.  Speak to me directly robin@clanmills.com or Skype me on 'clanmills' if you're interested.  I suspect you are in the UK, right?

4 days, 9 hours ago

Hillegass 2nd and 3rd Editions. Which one?, by maddogandnoriko

Ok, so I reached a point I really needed to read the book. So I went in search of Aaron's book, but I found a problem. I am on an older mac running 10.4.11, and am not going to upgrade just yet, that is a whole 'nother discussion. Regardless, I saw the 2nd edition covered OS X 10.3. And the 3rd edition covers 10.4-10.5. Huh? They are different though. The garbage collection was introduced in 10.5 I think, also I think Xcode is set up different, Interface builder is different. Also didn't Obj-c go to Obj-c2?

I am new so maybe those things don't really affect me. I believe Garbage collection is optional, and XCode and Interface Builder look to me like they are just different on the surface and basically the same.

Could anyone tell me which version I need to use?

Todd

5 days, 9 hours ago

Code for Saving a Document, by zophtx

sup i got a nice program that i am working on it almost done. couple things missing.
the most important thing that is missing is that i need to save the file. and than open it to where it was before.. (you know how saving works) 

but i would like to know what the code is soo i can save my program. or how to do it. thanks.

1 week, 2 days 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

1 week, 2 days 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.

1 week, 3 days 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


1 week, 3 days 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

1 week, 4 days 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). 

1 week, 5 days 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.

1 week, 5 days 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;
}

1 week, 5 days 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, 6 days 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, 6 days 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, 6 days 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, 6 days 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, 6 days 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 

2 weeks 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

2 weeks, 1 day 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

2 weeks, 1 day 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.

2 weeks, 1 day 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, 6 days 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, 6 days 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. 

3 weeks, 1 day 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.

3 weeks, 4 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.