Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
How do I get my program to open a webpage in the default browser when a button is clicked? Thanks! Nate
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
Ugh I hate their documentation because I still can't get through to it. Here's my latest variation of code: (I tried other ways too)

#import "OpenURL.h"

@implementation OpenURL
BOOL z;
- (IBAction)buttonPress:(id)sender {
[z openURL:(NSURL @"http://www.apple.com/")];
}
@end

errors:
error: syntax error before 'OBJC_STRING' token
warning: invalid receiver type 'BOOL'
warning: cast to pointer from integer of different size

Can someone please explain to me once and for all how to understand the documentation? They always put the stuff in the form of a method but I always need it in the form of a function.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Please use code tags!

Code:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.apple.com"]]

I'm not sure what your specific complaints about the documents are. They tell you if a method is an instance method or class method, then you just have to call the things with the parameters specified. It takes some getting used to, but it isn't so bad.

-Lee

P.S. It returns a BOOL. So you would have:
Code:
BOOL appOpened = [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.apple.com"]];
if(appOpened) {
  NSLog(@"Enjoy your page!");
} else {
  NSLog(@"Couldn't find an appropriate application to display a web page.");
}
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
The reason they put it "in the form of a method" is because it is a method. Cocoa is not a functional paradigm, it's an object oriented one. If you can't understand the documentation you need to go back to the start and learn how to use Cocoa properly.

Why would you think you can pass z, a BOOL, a message? What would you expect this to do? Your code is totally nonsensical.

I was about to post the correct code but I see lee1210 has already done...
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
I suppose it's a bad habit from my BASIC days... If this were put into the BASIC I'm used to, it would be something like this:

%boolean% = OPENURL("www.apple.com",Safari.app)

Anything that returns a number required a variable and = placed before it. Then you would use the value put into the variable to decide whether it went right or whatever.

Another problem is a difference in terminology. Anything returning a number to me is called a function. Then again in another BASIC a function was defined as "creating your own command", also known as a subroutine. And a method was the same as a function, only it was for is in "types" (objects)

So I have a lot of hard obstacles to get across. I was programming like that for years and now I have to not only know what new stuff means, but I need to rethink what things I already knew so well mean.

My simple understanding of methods and functions is that methods are "actions" and functions are "commands". I don't understand "void" methods. I use "IBActions" and functions to do what I want to do. Maybe I should hold off on learning that stuff because I'm not ready or maybe I should get it over with. I don't know. Thanks a lot for your help guys I really appreciate it :)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
My simple understanding of methods and functions is that methods are "actions" and functions are "commands". I don't understand "void" methods. I use "IBActions" and functions to do what I want to do. Maybe I should hold off on learning that stuff because I'm not ready or maybe I should get it over with. I don't know. Thanks a lot for your help guys I really appreciate it :)

Your basic understanding it flawed. Go and read up on Object-Oriented programming. The language doesn't matter, the basic concepts do. Basically when you have something like [obj methodName] this says we are passing a message called methodName to object obj. obj is not what gets returned from the call: it specifies what we are calling the method on. Almost all of Cocoa works by passing messages to objects (these are methods). There are almost no functions.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
The C convention is that even if an expression returns a value, that value can be ignored. In this case the openURL method DOES return a value, and you can have a variable that you assign the result to per my example. In this case you could evaluate the return immediately in an if rather than using a variable if you'd like.

In any case, passing a message to an object in Objective-C is an expression whose value is that of the return value of the method.

The terminology isn't as important as understanding (though knowing the terminology will help you express yourself more clearly on boards like this). When you pass a message in Objective-C you are wanting the class or instance you pass the message to to do something. The vast majority of the time that something will involve giving you a result as the return type of the method. There are functions that do not relate to any class/object like NSLog (and anything in a C library you might want to use, as well). These may return a value or may be void as well.

In terms of what you can assign to what using the assignment operator, =, I don't know if this is the time to talk about rvalues and lvalues in C. We'll just work off of the idea that when your expression is a method call its value can be assigned to something, but you can't assign something to it.

Don't worry, you can recover from BASIC, but I would do my best if I were you to approach a new topic with fresh eyes. Don't try to cram it in to the paradigm of BASIC, it just won't work.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.