ios - 自定义应用程序默认 NSDictionary

标签 ios objective-c react-native react-native-ios

我正在尝试设置 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 framework不是很了解)。您不必为此使用 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;
}

关于ios - 自定义应用程序默认 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43615347/

相关文章:

javascript - react : Filter or map Object by iterating through list of objects

android - react native 编译报错android - react-native-webview

javascript - 屏蔽react-native TextInput

ios - 我应该对 Apple Developer Team ID 保密吗?

ios - 我想保存应用程序,这样当用户退出时,用户返回时 uitextfields 不会删除为文本

objective-c - 如何开始在 iOS 上使用 ARM?

objective-c - 如何在不覆盖 objective-c 的情况下保存文本文件

ios - 从 Objective-C 中的货币代码获取货币符号

ios - 为 iPhone 相机设置白平衡和曝光模式 + 枚举默认值

ios - Instagram 应用程序权限被拒绝