NSURL で NSInvalidArgumentException !?

UIWebView shouldStartLoadWithRequest の中で、要求されたURLでなくて他の URL をロードさせる場合、

  -[NSURL length]: unrecognized selector sent to instance

というエラーで失敗することがある。

例)以下のように何も問題なさそうな URLを編集してリクエストをするケースで

@property (nonatomic,retain) UIWebView* webView;
@synthesize webView;

self.webView = [[UIWebView alloc] init];

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request 
 navigationType:(UIWebViewNavigationType)navigationType

{
   // 元のURLの一部文字列を置換
   NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/alpha/" options:0 error:nil ];
   NSString *replaceUrlString = [NSURL URLWithString:[regex stringByReplacingMatchesInString:request.URL.absoluteString
                                                                                     options:NSMatchingReportProgress
                                                                                       range:NSMakeRange(0, request.URL.absoluteString.length)
                                                                                withTemplate:@"/beta/" ]
                                ];
   // 改めてリクエスト
   [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:replaceUrlString]] ];


iOS 4.3 では、以下のエラー


-[NSURL length]: unrecognized selector sent to instance 0x4e6ae80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException'
, reason: '-[NSURL length]: unrecognized selector sent to instance 0x4e6ae80'


iOS 6.0 では、以下のエラー


-[NSURL length]: unrecognized selector sent to instance 0x75a9470
*** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener:
 delegate: <NSInvalidArgumentException> -[NSURL length]: unrecognized selector sent to instance 0x75a9470

-------------
なんで、メモリ参照のエラーになるのか??

仕方ないので、一旦、URL の NSString を作ってから渡すことに。。


// 元のURLの一部文字列を置換
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/alpha/" options:0 error:nil ];
NSString *replaceUrlString = [NSURL URLWithString:[regex stringByReplacingMatchesInString:request.URL.absoluteString
                                                                                  options:NSMatchingReportProgress
                                                                                    range:NSMakeRange(0, request.URL.absoluteString.length)
                                                                             withTemplate:@"/beta/" ]
                                ];

NSString *forceUrlstring = [NSString stringWithFormat:@"%@", 
replaceUrlString];

// 改めてリクエスト
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:forceUrlstring]] ];

とすれば、エラーにならない。