Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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
Ios NSURL模拟到CFURLCreateCopyAppendingPathComponent?_Ios_Objective C - Fatal编程技术网

Ios NSURL模拟到CFURLCreateCopyAppendingPathComponent?

Ios NSURL模拟到CFURLCreateCopyAppendingPathComponent?,ios,objective-c,Ios,Objective C,使用URL,更具体地说,是从其他已发现的URL增量构建URL。在这样做的过程中,我希望继续使用NSURL对象,而不是操纵NSString,只是为了从url类中获得附加的健全性检查和特定于url的方法 不幸的是,似乎无法按照我的意愿将以下内容结合起来: NSURL *base = [NSURL URLWithString:@"http://my.url/path"]; NSString *suffix = @"sub/path"; NSURL*base=[NSURL URLWithString:@

使用URL,更具体地说,是从其他已发现的URL增量构建URL。在这样做的过程中,我希望继续使用NSURL对象,而不是操纵NSString,只是为了从url类中获得附加的健全性检查和特定于url的方法

不幸的是,似乎无法按照我的意愿将以下内容结合起来:

NSURL *base = [NSURL URLWithString:@"http://my.url/path"]; NSString *suffix = @"sub/path"; NSURL*base=[NSURL URLWithString:@”http://my.url/path"]; NSString*后缀=@“子/路径”; 我想附加它们以获得:

http://my.url/path/sub/path 但我能得到的最好结果似乎是:

NSURL *final = [NSURL URLWithString:suffix relativeToURL:base]; NSURL*final=[NSURL URLWithString:后缀relativeToURL:base]; 这会修剪基座上的路径,从而导致:

http://my.url/sub/path 有一个CoreFoundation函数可以实现这一点:

CFURLRef CFURLCreateCopyAppendingPathComponent ( CFAllocatorRef allocator, CFURLRef url, CFStringRef pathComponent, Boolean isDirectory ); CFURLRef CFURLCreateCopyAppendingPathComponent( CFAllocatorRef分配器, CFURLRef url, CFStringRef路径组件, 布尔isDirectory );
这似乎工作得很好,但从ObjC到C的来回跳转是不和谐和恼人的。。。我宁愿只是操纵字符串。。。我错过什么了吗?当然,除了过于挑剔之外。:)

您可以将URL转换为字符串,附加相对组件,然后将其转换回URL:

NSURL *base = [NSURL URLWithString:@"http://my.url/path"];
NSString *suffix = @"/sub/path"; //Note that you need the initial forward slash

NSString *newURLString = [[base absoluteString] stringByAppendingString:suffix];
NSURL *newURL = [NSURL URLWithString:newURLString];

旁注:如果URL恰好是一个文件(不是在您最初的示例中,但在某些情况下可能会有所帮助),您也可以使用NSString的
stringByAppendingPathComponent:
方法-这将使您无法为基本路径和相对路径选择正确的分隔符。

这是目标C-如果NSURL不能满足您的需要,请添加扩展类别

@interface NSURL ( Extensions )
- (NSURL*) urlByAppendingPathComponent: (NSString*) component;
@end

@implementation NSURL ( Extensions )
- (NSURL*) urlByAppendingPathComponent: (NSString*) component;
{
    CFURLRef newURL = CFURLCreateCopyAppendingPathComponent( kCFAllocatorDefault, (CFURLRef)[self absoluteURL], (CFStringRef)component, [component hasSuffix:@"/"] );
    return [NSMakeCollectable(newURL) autorelease];
}
@end
然后:

*base = [NSURL URLWithString:@"http://my.url/path"];
*suffix = @"sub/path";
NSURL *final = [base urlByAppendingPathComponent:suffix];
NSLog( @"%@", final );
// displays http://my.url/path/sub/path

从iOS 4.0开始,NSURL类通过AppendingPathComponent:方法提供了一个
URLByAppendingPathComponent:
方法。

我通过将斜杠从相对url的开头移动到基本url的结尾来解决这个问题,如下所示:

NSURL *base = [NSURL URLWithString:@"http://my.url/path/"]; //added a slash to the end
NSString *suffix = @"sub/path"; //seems you don't have the leading slash in your sub path so you don't have to change this line

这是一个非常糟糕的主意,因为您必须自己完全正确地处理斜杠的处理。加上路径后面有任何内容的URL(查询字符串等)都会被严重损坏。需要注意的一点是CoreFoundation如何处理相对URL(既有-baseURL又有-relativeString的URL)。你可能想做(CFURLRef)[自我绝对URL]来代替。好建议迈克,我做了调整+1-一个很好的建议。如果我可以稍微吹毛求疵的话,也许选择一个比“扩展”更具体的类别名称可能会有助于澄清。非常感谢您我很高兴这有帮助,如果你认为这是一个好的答案,请投我一票,这样其他人就更有可能看到它