自定义应用程序默认 NSDictionary

Custom appsDefaults NSDictionary

我正在尝试设置 react-native 应用程序,但我在对 AooDekegate.m 文件进行真正的基本更改时遇到了困难。我没有 xCode 的经验。我不知道我在做什么。

我正在学习本教程 http://www.proreactnative.com/How-to-Develop-iOS-Apps-on-Linux-Using-React-Native/。我开始做 Edit AppDelegate.m.

你能帮帮我吗?

这是我的代码:

/** * 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

您确实在函数之外初始化了一个数据对象,这对某些类型来说没问题。

在这种情况下,我宁愿将给定值声明为常量,或者只是在使用变量的方法中声明:

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

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

// Rest of your code
...
}

另外:

如果你以后不需要这些值(我对react native框架不是很了解)。您不必为此使用 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;
}