简体   繁体   English

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

[英]string manipulation and methods in obj-c

I am very new to obj-c (about 1 day) and i have read the documentation on how to call methods and how to modify strings and i have used similar code in another program and it worked fine.我对 obj-c 非常陌生(大约 1 天),我已经阅读了有关如何调用方法和如何修改字符串的文档,并且我在另一个程序中使用了类似的代码,它运行良好。 I'm programming a simple web browser for the iphone to teach myself about WebViewController library.我正在为 iphone 编写一个简单的 web 浏览器,以自学 WebViewController 库。 When i compile this it gives me the warning "'WebViewController' may not respond to '-parseURl:" at line 17 in the.m file and when i run it i throws the error "NSInvalidArgumentException" in the console.当我编译它时,它在.m 文件的第 17 行给出警告“'WebViewController' 可能无法响应'-parseURl:”,当我运行它时,我在控制台中抛出错误“NSInvalidArgumentException”。

Code for this in WebViewController.h: 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

Code for this in WebViewController.m: 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

Thanks for the help谢谢您的帮助

Objective-C (and most other languages) is case-sensitive. Objective-C(和大多数其他语言)区分大小写。 "URL" and "URl" are different. “URL”和“URl”是不同的。

urlAddress = [self parseURl:urlAddress];

should be应该

urlAddress = [self parseURL:urlAddress];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM