Iphone 如何在一个文件中重复单词并使用它
我想提出两个相同的申请。这两个应用程序的区别在于徽标和名称(以及联系我们页面) 考虑两个名称是名称1和名称2,我想在任何地方都使用它们。假设下面是我的文本Iphone 如何在一个文件中重复单词并使用它,iphone,ios,objective-c,Iphone,Ios,Objective C,我想提出两个相同的申请。这两个应用程序的区别在于徽标和名称(以及联系我们页面) 考虑两个名称是名称1和名称2,我想在任何地方都使用它们。假设下面是我的文本 Welcome to name_here name_here is founded in 1987. name_here is blah blah.. name_here is blah blah... 在这里,我想用名称1和名称2分别替换每个应用程序的名称_ 你知道怎么做吗 这就是我所做的 将plist文件创建为“AllInOne.
Welcome to name_here
name_here is founded in 1987. name_here is blah blah.. name_here is blah blah...
在这里,我想用名称1和名称2分别替换每个应用程序的名称_
你知道怎么做吗
这就是我所做的 将plist文件创建为“AllInOne.plist” 在我看来
NSString *documentsDirectory = [[NSBundle mainBundle] resourcePath];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"AllInOne.plist"];
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString *value1;
value1 = [savedStock objectForKey:@"lab_name"];
NSLog(@"text is ==== %@", value1);
就这样。。。非常感谢…请尝试以下代码
#define NAME1 @"Name1"
#define NAME2 @"Name2"
NSString *str = [NSString stringWithFormat:@"Welcome to %@ %@ is founded in 1987. %@ is blah blah.. %@ is blah blah...",NAME1,NAME1,NAME1,NAME1];
谢谢,您可以在应用程序的.plist中创建一个类似appName的字段,并根据应用程序将该变量的值设置为Name1或Name2
然后,您只需从plist中检索该变量。解决方案可能是使用本地化字符串 将常数定义为:
#define MY_NAME NSLocalizedStringFromTable(@"name", @"Localizable", nil)
然后添加一个Localizable.strings文件,您可以在其中写入:
"name" = "name1"
或
(取决于您的应用程序)您应该将变量数据(在本例中为名称变量)存储在一个外部文件中,以便可以为每个应用程序进行配置。它可能是与应用程序捆绑在一起的JSON或plist文件,或者您可以通过web服务等获取它。我的建议是,您将名称声明为常量,无论出现什么差异,都可以替换变量,而不是硬编码名称
#define name @"Name"
NSString *str = [NSString stringWithFormat:@"Welcome to %@. %@ is founded in 1987. %@ is this and that..."],name;
在其他应用程序中,您将名称定义为其他字符串,如下所示
#define name @"SomeOtherName"
您可以对需要重用的命名徽标或其他资源执行相同的操作。实际上,最好保留一个类,只存储这些名称和常量。因此,如果操作正确,只需删除此类文件并添加另一个名称相同的类文件,该类文件包含相同的常量,但这些常量中存储的值不同。您可以为每个应用定义一个预处理器,例如APP1和APP2。 然后,无论在何处设置name_的字符串,都可以粘贴以下代码:
#ifdef APP1
name_here = @"Company 1";
#else
name_here = @"Company 2";
用户看到的字符串应该位于类似“Localizable.Strings”的文件中 然后使用: self.title=NSLocalizedStringFromTable(@“应用程序名称”,“应用程序”,“应用程序”)
本地化还允许占位符 我没有很好但有效的答案:
NSString * string = @"Welcome to name_here, name_here is founded in 1987. name_here is blah blah.. name_here is blah blah... ";
#if APP1
string = [string stringByReplacingOccurrencesOfString:@"name_here" withString:APP1Text];
#else
string = [string stringByReplacingOccurrencesOfString:@"name_here" withString:APP2Text];
您需要获取整个字符串“name_here”,并用您想要的特定文件中的字符串替换。是的,我认为这将是一个很好的选择。
NSString * string = @"Welcome to name_here, name_here is founded in 1987. name_here is blah blah.. name_here is blah blah... ";
#if APP1
string = [string stringByReplacingOccurrencesOfString:@"name_here" withString:APP1Text];
#else
string = [string stringByReplacingOccurrencesOfString:@"name_here" withString:APP2Text];