【问题标题】:No visible @interface for 'NSString' declares the selector URLEncodeUsingEncoding'NSString' 没有可见的@interface 声明选择器 URLEncodeUsingEncoding
【发布时间】:2016-10-26 10:19:54
【问题描述】:

我是 ios 新手,反对 C.

我正在尝试对 url 进行编码,但出现了错误

/Users/zupportdesk/Desktop/MyIOSApps/ZDticketSystem/ZDticketSystem/ViewController.m:313:52: 'NSString' 没有可见的@interface 声明选择器 'URLEncodeUsingEncoding:'

#import "ViewController.h"
#import "AppDelegate.h"
#import "ApplicationEnvironmentURL.h"
#import "AFNetworking/AFNetworking.h"
#import "HHAlertView/HHAlertView.h"
#import "MBProgressHUD/MBProgressHUD.h"
#import "AppDelegate.h"


@interface ViewController ()<UITextFieldDelegate, UIActionSheetDelegate> {
    ApplicationEnvironmentURL *applicationEnvironment;
    NSString *BASEURL;
    NSString *global_profileId;
    AppDelegate *delegate;
}

@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *password;
@end

@implementation ViewController
--- defaults methods here

- (void)setupProfileidURL:(NSString *)profileToken {
    NSString *encoded_profileToken = [profileToken URLEncodeUsingEncoding:NSUTF8StringEncoding];
    NSString *user_login_url = [applicationEnvironment get_user_api_url];
    BASEURL = [[user_login_url stringByAppendingString:@"Account/GetProfileDetails?profileToken="] stringByAppendingString:encoded_profileToken];
    NSLog(@"Base URL: %@", BASEURL);
}

我正在调用这样的方法 [self setupProfileidURL:profileToken];

查看Holder头文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *tv_username;
@property (weak, nonatomic) IBOutlet UITextField *tv_password;

@end

我想实现这个 https://madebymany.com/blog/url-encoding-an-nsstring-on-ios 。有人可以帮我处理这个 url 编码吗?

【问题讨论】:

  • 您必须为实现-URLEncodeUsingEncoding:NSString 创建一个类别,并将该类别导入您需要的位置。请参阅此处了解如何创建类别:stackoverflow.com/questions/24324021/…

标签: ios objective-c nsstring


【解决方案1】:

你收到这条消息

“NSString”没有可见的@interface 声明选择器 'URLEncodeUsingEncoding:'

因为URLEncodeUsingEncoding 是一个在类别中定义的方法,而您没有在您的类中使用该类别,这就是您收到此消息的原因。

此外,CFURLCreateStringByAddingPercentEscapes 已从 iOS 中弃用,因此您可以使用 stringByAddingPercentEncodingWithAllowedCharacters 代替它

以下是您的功能示例

- (void)setupProfileidURL:(NSString *)profileToken {
    NSString *encoded_profileToken = [profileToken stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *user_login_url = [applicationEnvironment get_user_api_url];
    BASEURL = [[user_login_url stringByAppendingString:@"Account/GetProfileDetails?profileToken="] stringByAppendingString:encoded_profileToken];
    NSLog(@"Base URL: %@", BASEURL);
} 

此外,根据@Larme 评论,您可以创建一个类别并通过导入它在您的应用程序中使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多