gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:10:31 25 4
gpt4 key购买 nike

我正在尝试设置 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com