2012-02-28 1 views
0

何かのようにinet_addr("127.0.0.1")iOSでIPアドレスを4つの整数に解析する機能はありますか?

+1

あなたは 'のinet_addr( "127.0.0.1")を'試したことがありますか? –

+0

@RobNapier明らかにinet_addrは廃止されました。代わりにinet_ptonを使用するべきです。 –

+1

しないでください。これはIPv6アドレスでは機能しなくなります。あなたが解決しようとしている実際の問題は何ですか? –

答えて

3
NSString *ipAddress = [NSString stringWithString:@"127.0.0.1"]; 

NSArray *results = [ipAddress componentsSeparatedByString:@"."]; 
if ([results count] == 4) { 
    NSLog(@"1 - %d", [[results objectAtIndex:0] intValue]); 
    NSLog(@"2 - %d", [[results objectAtIndex:1] intValue]); 
    NSLog(@"3 - %d", [[results objectAtIndex:2] intValue]); 
    NSLog(@"4 - %d", [[results objectAtIndex:3] intValue]); 
} else { 
    NSLog(@"not valid ip"); 
} 
+0

"有効でないIP"ですか? IPv6についてはどうですか? –

+0

inet.hファイルのsin_addrフィールドを掘り下げてみると、トピックの質問 – NeverBe

0

あなたは私はあなたがおそらくString Programming Guideをチェックアウトするべきだと思い

#include <arpa/inet.h> 
+0

はありません。見つかったのは、32ビットintでサポートされているということだけです。私はアドレスのコンポーネントを引き出すマクロを見つけることができないようです。彼らはどこかに存在すると確信していますが、どこで? –

+0

sin_addrフィールドからIPコンポーネントを抽出する標準的な方法はないと私は考えています。 http://www.retran.com/beej/sockaddr_inman.htmlによれば、彼は基本的に "ここにドラゴンズがいる"と言っています。 –

0

を含める必要が

inet_pton(AF_INET, "239.255.255.250", &broadcastAddr.sin_addr); 

のようなものを呼び出す必要があります。それは単純なものであるならば、あなただけの

... [yourSting [email protected]"."]; //('formatting String Objects' section) 

ような何かを行うことができたり、あなたが望むもののために、関連する文字列を検索できるように、Appleのサンプル・コード(「スキャナセクション」)から何かenvolvingスキャナーを行うことができます:

NSString *string = @"Product: Acme Potato Peeler; Cost: 0.98 73\n\ 
Product: Chef Pierre Pasta Fork; Cost: 0.75 19\n\ 
Product: Chef Pierre Colander; Cost: 1.27 2\n"; 

NSCharacterSet *semicolonSet; 
NSScanner *theScanner; 
NSString *PRODUCT = @"Product:"; 
NSString *COST = @"Cost:"; 
NSString *productName; 
float productCost; 

NSInteger productSold; 
semicolonSet = [NSCharacterSet characterSetWithCharactersInString:@";"]; 
theScanner = [NSScanner scannerWithString:string]; 
while ([theScanner isAtEnd] == NO) 
{ 
if ([theScanner scanString:PRODUCT intoString:NULL] && 
[theScanner scanUpToCharactersFromSet:semicolonSet 
intoString:&productName] && 
[theScanner scanString:@";" intoString:NULL] && 
[theScanner scanString:COST intoString:NULL] && 
[theScanner scanFloat:&productCost] && 
[theScanner scanInteger:&productSold]) 
{ 
NSLog(@"Sales of %@: $%1.2f", productName, productCost * productSold); 
} 
} 

グッドラック

関連する問題