2017-04-25 11 views
0

反応ネイティブアプリケーションを設定しようとしていますが、実際にはAooDekegate.mファイルの基本的な変更を行いました。 xCodeには経験がありません。私は何をしているのか分かりません。カスタムアプリケーションデフォルトNSDictionary

私はこのチュートリアルhttp://www.proreactnative.com/How-to-Develop-iOS-Apps-on-Linux-Using-React-Native/に従っていました。私はEdit AppDelegate.mをやっているstuctを持っています。

私を助けてください。

これは私のコードです:

/** * Copyright (c) 2015-present, Facebook, Inc. 
* All rights reserved. 
* 
* This source code is licensed under the BSD-style license found in the 
* LICENSE file in the root directory of this source tree. An additional grant 
* of patent rights can be found in the PATENTS file in the same directory. 
*/ 

#import "AppDelegate.h" 

#import <React/RCTBundleURLProvider.h> 
#import <React/RCTRootView.h> 

@implementation AppDelegate 

NSDictionary *appDefaults = @{ // ERROR !! Initializer element is not a compile-time constant 
    @"host_preference": @"localhost", 
    @"port_preference": @"8081", 
}; 
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; // ERROR !! Expected ']' 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
NSURL *jsCodeLocation; 

NSString *host = [[NSUserDefaults standardUserDefaults] stringForKey: @"host_preference"]; 
NSString *port = [[NSUserDefaults standardUserDefaults] stringForKey: @"port_preference"]; 

NSString * urlString = [NSString stringWithFormat: @"http://%@:%@/index.ios.bundle?platform=ios&dev=true", host, port]; 
jsCodeLocation = [NSURL URLWithString: urlString]; 

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation 
                moduleName:@"AwesomeProject" 
               initialProperties:nil 
                launchOptions:launchOptions]; 
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; 

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
UIViewController *rootViewController = [UIViewController new]; 
rootViewController.view = rootView; 
self.window.rootViewController = rootViewController; 
[self.window makeKeyAndVisible]; 
return YES; 
} 

@end 

答えて

2

あなたが特定の種類の罰金されるであろう、関数の外にデータオブジェクトを初期化します。また

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

NSDictionary *appDefaults = @{ 
    @"host_preference": @"localhost", 
    @"port_preference": @"8081", 
}; 
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; 

// Rest of your code 
... 
} 

:この場合

、私はむしろ定数として、または単にあなたの変数を使用する方法で与えられた値を宣言し

あなたは、これらの値を必要としない場合後でもう一度(私は反応するネイティブフレームワークを詳しく知らない)。このためにNSUserDefaultsを使用する必要はありません。 (NSUserDefaultsはアプリケーションのデータを永続化しようとする試みです)。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSURL *jsCodeLocation; 
    NSString *host = @"localhost"; 
    NSString *port = @"8081"; 

    NSString * urlString = [NSString stringWithFormat: @"http://%@:%@/index.ios.bundle?platform=ios&dev=true", host, port]; 


    jsCodeLocation = [NSURL URLWithString: urlString]; 

    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation 
                moduleName:@"AwesomeProject" 
               initialProperties:nil 
                launchOptions:launchOptions]; 

    rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; 

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    UIViewController *rootViewController = [UIViewController new]; 
    rootViewController.view = rootView; 
    self.window.rootViewController = rootViewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+0

ハ!どうもありがとう。それは働いているように見えます。私はそれが愚かな間違いのように見えるかもしれませんが、私はCやxCodeの構文がどのように見えるか分かりません。 – Michal

+0

あなたは歓迎Michalです。あなたを助けてくれてうれしいです。 – Lepidopteron

+1

@Michalちょっとした情報を追加するには、いくつかの変数を置く場所で宣言できますが、 'static'または' const'である必要があります。特定のエラー 'Initializer要素はコンパイル時定数ではありません.'では単にその場所に**定数**を宣言する必要があると言います。あなたの 'NSDictionary'は、内部の要素を変更できるので、定数ではありません。しかし、静的なNSString * someString = @ "HELLO"を宣言すれば、その文字列は時間的に変化しないので、動作します。同じ文字列インスタンスが、あなたの異なるAppDelegateインスタンスのすべてに渡されます(この特定のクラスのインスタンスだけ)。 –