Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用多个#define语句创建URL_Iphone_Objective C_Ipad - Fatal编程技术网

Iphone 使用多个#define语句创建URL

Iphone 使用多个#define语句创建URL,iphone,objective-c,ipad,Iphone,Objective C,Ipad,我想在我的应用程序中定义一个基本URL: #define kBaseURL @"http://mydomain.com/" 用户也可以使用这个作为基础来创建其他URL #define kProductsURL @"/products" 我知道我可以这样做: NSString *urlString = [NSString stringWithFormat:@"%@%@", kBaseURL,kProductsURL]; 如何更改kProductsURL我只能使用kProductsURL进行字

我想在我的应用程序中定义一个基本URL:

#define kBaseURL @"http://mydomain.com/"
用户也可以使用这个作为基础来创建其他URL

#define kProductsURL @"/products"
我知道我可以这样做:

NSString *urlString = [NSString stringWithFormat:@"%@%@", kBaseURL,kProductsURL];
如何更改
kProductsURL
我只能使用
kProductsURL
进行字符串格式化

我知道你可以用
#define
语句做很多事情,但我还没有找到好的教程或文档


有人能给我指出正确的方向吗?

你可以将
#定义
简单如下:

#define kBaseURL     @"http://mydomain.com"
#define kProductsURL kBaseURL@"/products"

// kProductsURL becomes http://mydomain.com/products

编辑:以其他答案和评论为基础

但是考虑使用<代码>定义< <代码>是代码的味道。另一种方法是使用

const
s。这比使用
#define
具有优势,因为当您编辑
常量的内容时,它不需要编译整个项目

url.h
文件:

extern NSString * const kBaseURL;
extern NSString * const kProductsPath;
NSString *GenerateMyURL(NSString *path);
NSString * const kBaseURL = @"http://mydomain.com";
NSString * const kProductsPath = @"/products";

NSString * GenerateMyURL(NSString *path)
{
    return [kBaseURL stringByAppendingPathComponent:path];
}
url.m
文件:

extern NSString * const kBaseURL;
extern NSString * const kProductsPath;
NSString *GenerateMyURL(NSString *path);
NSString * const kBaseURL = @"http://mydomain.com";
NSString * const kProductsPath = @"/products";

NSString * GenerateMyURL(NSString *path)
{
    return [kBaseURL stringByAppendingPathComponent:path];
}
用法:

NSString *productsURL = GenerateMyURL(kProductsPath);
// Do whatever...

您可以按如下方式连接
#定义
s:

#define kBaseURL     @"http://mydomain.com"
#define kProductsURL kBaseURL@"/products"

// kProductsURL becomes http://mydomain.com/products

编辑:以其他答案和评论为基础

但是考虑使用<代码>定义< <代码>是代码的味道。另一种方法是使用

const
s。这比使用
#define
具有优势,因为当您编辑
常量的内容时,它不需要编译整个项目

url.h
文件:

extern NSString * const kBaseURL;
extern NSString * const kProductsPath;
NSString *GenerateMyURL(NSString *path);
NSString * const kBaseURL = @"http://mydomain.com";
NSString * const kProductsPath = @"/products";

NSString * GenerateMyURL(NSString *path)
{
    return [kBaseURL stringByAppendingPathComponent:path];
}
url.m
文件:

extern NSString * const kBaseURL;
extern NSString * const kProductsPath;
NSString *GenerateMyURL(NSString *path);
NSString * const kBaseURL = @"http://mydomain.com";
NSString * const kProductsPath = @"/products";

NSString * GenerateMyURL(NSString *path)
{
    return [kBaseURL stringByAppendingPathComponent:path];
}
用法:

NSString *productsURL = GenerateMyURL(kProductsPath);
// Do whatever...

您可以这样做:

#define kBaseURL(extension)  [NSString stringWithFormat:@"http://mydomain.com/%@", extension]
下一步在代码中使用:

NSString *urlString = kBaseURL(kProductsURL);
// urlString becomes http://mydomain.com/products

++

您可以这样做:

#define kBaseURL(extension)  [NSString stringWithFormat:@"http://mydomain.com/%@", extension]
下一步在代码中使用:

NSString *urlString = kBaseURL(kProductsURL);
// urlString becomes http://mydomain.com/products

++

对于这个简单的例子,我更喜欢
NSString*const baseURL=@”http://mydomain.com/";
甚至可能在某个标题中声明它(以隐藏实际值),对于这种简单的情况,我更喜欢
NSString*const baseURL=@”http://mydomain.com/";
甚至可能在某个标题中声明它
extern
(隐藏实际值)请使用
stringByAppendingPathComponent
而不是
stringWithFormat
请使用
stringByAppendingPathComponent
而不是
stringWithFormat
您还可以使它成为真正的函数,也许NS_内联并将其放在标题中。。。只是想避免#定义。。。还可以包括逻辑,以这种方式导出基本url(基于品牌或本地化)。您还可以将其变成一个真正的函数,可能是NS_内联并将其放入标头中。。。只是想避免#定义。。。还可以包括以这种方式导出基本url的逻辑(基于品牌或本地化)。