obj-c中的字符串操作和方法

时间:2011-04-30 19:54:34

标签: objective-c

我是obj-c的新手(大约1天),我已经阅读了有关如何调用方法以及如何修改字符串的文档,并且我在其他程序中使用了类似的代码,并且工作正常。我正在为iphone编写一个简单的Web浏览器来自学WebViewController库。当我编译它时,它给我警告“'WebViewController'可能不会响应'-parseURl:”在.m文件的第17行,当我运行它时,我在控制台中抛出错误“NSInvalidArgumentException”。

WebViewController.h中的代码:

#import <UIKit/UIKit.h>


@interface WebViewController : UIViewController {

    IBOutlet UIWebView *webView;
    IBOutlet UITextField *textField;
}

NSString *urlAddress;
NSURL *url;
NSURLRequest *requestObj;

- (IBAction)gotoAddress:(id)sender;
- (NSString*) parseURL:(NSString*)str;

@property (nonatomic, retain) UIWebView *webView;

@end

WebViewController.m中的代码:

#import "WebViewController.h"


@implementation WebViewController

@synthesize webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Initialization code
    }
    return self;
}

- (IBAction)gotoAddress:(id)sender {
    urlAddress = textField.text;
    urlAddress = [self parseURl:urlAddress];
    url = [NSURL URLWithString:urlAddress];
    requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
    NSLog(@"urlAddress= %s", [urlAddress cStringUsingEncoding:1]);
}


- (NSString*) parseURL:(NSString*)str {
    NSLog(@"made it");
    NSString *httpPart = @"http://";
    if ([str rangeOfString:httpPart].location == NSNotFound) {
        NSString *correctURL = [NSString stringWithFormat:@"%@%@", httpPart, str];
        return correctURL;
    }
    else {
        return str;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [webView release];
    [super dealloc];
}


@end

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

Objective-C(和大多数其他语言)区分大小写。 “URL”和“URl”不同。

urlAddress = [self parseURl:urlAddress];

应该是

urlAddress = [self parseURL:urlAddress];