ios-怎么样让不同变量重复一个动作?

问题描述

怎么样让不同变量重复一个动作?
举例说明我想实现的,比如我有十个变量(整数值)以及变量的值,一个字符串。

然后代码中是判断云的数量决定天气状况:

 if (hour1cloud <= 5) {            hour1weather = @""Clear"";        }        if (5 < hour1cloud <= 25) {            hour1weather = @""Mostly Clear"";        }        if (25 < hour1cloud <= 50) {            hour1weather = @""Partly Cloudy"";        }        if (50 < hour1cloud <= 83) {            hour1weather = @""Mostly Cloudy"";        }        if (83 < hour1cloud <= 105) {            hour1weather = @""Overcast"";        }

然后 hour2cloud hour3cloud hour4cloud分别对应hour2weather hour3weather等。当我输入hour1cloud获取hour1weather的值,能不能对所有组都变成通用方法?

解决方案

方法如下:

- (NSString*)weatherStringFromCloud:(int)cloud {    NSString *weather;    if (cloud <= 5) {        weather = @""Clear"";    } else if (cloud <= 25) {        weather = @""Mostly Clear"";    } else if (cloud <= 50) {        weather = @""Partly Cloudy"";    } else if (cloud <= 83) {        weather = @""Mostly Cloudy"";    } else if (cloud <= 105) {        weather = @""Overcast"";    } else {        weather = nil;    }    return weather;}

然后用不同变量调用:

hour1weather = [self weatherStringFromCloud:hour1cloud];hour2weather = [self weatherStringFromCloud:hour2cloud];hour3weather = [self weatherStringFromCloud:hour3cloud];hour4weather = [self weatherStringFromCloud:hour4cloud];

解决方案二:

static NSString *stringForCloudiness(int cloudiness) {    static int const kCloudinesses[] = { 5 25 50 83 105 };    static NSString *const kStrings[] = { @""Clear"" @""Mostly Clear"" @""Partly Cloudy"" @""Mostly Cloudy"" @""Overcast"" };    static int const kCount = sizeof kCloudinesses / sizeof *kCloudinesses;    for (int i = 0; i < kCount; ++i) {        if (cloudiness <= kCloudinesses[i]) {            return kStrings[i];        }    }    return @""A cloudiness level unparalleled in the history of recorded weather"";}

接下来要同步数组:

static NSString *stringForCloudiness(int cloudiness) {    typedef struct {        int cloudiness;        __unsafe_unretained NSString *string;    } CloudStringAssociation;    static CloudStringAssociation const kAssociations[] = {        { 5 @""Clear"" }        { 25 @""Mostly Clear"" }        { 50 @""Partly Cloudy"" }        { 83 @""Mostly Cloudy"" }        { 105 @""Overcast"" }        { INT_MAX @""A cloudiness level unparalleled in the history of recorded weather"" }    };    int i = 0;    while (cloudiness > kAssociations[i].cloudiness) {        ++i;    }    return kAssociations[i].string;}
时间: 2024-07-31 08:17:12

ios-怎么样让不同变量重复一个动作?的相关文章

java 多线程方法加锁获取自增变量重复问题

问题描述 java 多线程方法加锁获取自增变量重复问题 /** 测试多线程并发获取唯一子增长的值 @author Administrator * */ public class BB { private int increment = 0; final static Set set = new HashSet(); final static List list = new ArrayList(); public synchronized int getauto() { return increm

static 静态变量-iOS 的static静态变量

问题描述 iOS 的static静态变量 你好,我定义了一个静态变量 static int timeout = self.timeNumber; //倒计时时间 self.timeNumber 是int 型的. 这样赋值是错误的,请问怎么赋值给静态变量不会出错 因为 我这个timeout变量必须是static,否则NStimer就不会启动 解决方案 C++ 中的static静态变量static静态变量静态变量static 解决方案二: http://blog.csdn.net/wbw1985/a

firefox下jquery iframe刷新页面提示会导致重复之前动作_jquery

刷新页面会提示 "要显示此页面, Firefox 必须发送将会导致重复之前动作的数据(例如搜索或者下订单)"看看以下代码 复制代码 代码如下: $("iframe").load(function(){ $(this).attr("src","about:blank"); }) 框架打开后设置地址到一个空页面就可以避免这个提示但有个问题 就是设置src后会触发load事件,会导致循环加载所以需要设置一个参数 等触发完正常事件后

ios开发中uiscrollview里嵌套一个uiscrollview

问题描述 ios开发中uiscrollview里嵌套一个uiscrollview ios开发中uiscrollview里嵌套一个uiscrollview 其中小得scrollview是一个用于放滚动图片的.大得scrollview是用于整个view滚动的..其中还有很多别的view譬如imageview等,现在遇到这样的问题:我滚动大得scrollview,放滚动图片的scroll不跟着动,就一直悬在固定的位置.求解 急呀 解决方案 如果小的uiscrollview是作为subview添加到外部

ios-在ISGL3D中,一次只识别一个动作

问题描述 在ISGL3D中,一次只识别一个动作 我目前在开发ISGL3D应用,要求一次只识别一个动作. 比如说,同一时间,只能识别PICNH或者PAN. 不知道应该用什么方法? 解决方案 用下面的方法可以一次只识别一个动作: - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognize

c++-在工具栏的一个动作上来显示提示信息怎么没看到?

问题描述 在工具栏的一个动作上来显示提示信息怎么没看到? 因为设置了工具提示,所以运行程序时可以将鼠标停在工具栏的一个动作上来显示提示 信息,这提示信息怎么样的,为什么我没看到? 解决方案 菜单条一般没有工具提示,你可以添加一个状态栏,然后把工具提示显示在底部的工具栏中.很多软件都是这么设计的.比如xp下的写字板. 解决方案二: 一个显示提示信息的对象WebControl.Attributes

ios开发中能不能用一个app去下载另一个app的安装包并自动安装

问题描述 ios开发中能不能用一个app去下载另一个app的安装包并自动安装 需求:有一个主APP,用来下载其他APP,比如下载完成后自行安装到主APP中(就像插件),然后会产生一个图标,点击这个图标就会启动新安装的APP,当安装这个APP后又返回到主APP中.---------有点像Iphone的桌面. 有没有大神知道类似的实现方法. 解决方案 你看看腾讯的qq农场,其中需要跳转到qq牧场的时候就和你的情况一样,ps:QQ农场和qq牧场是俩app 解决方案二: 一般来说都是跳到APP stor

游戏编程-LUA:前面的函数循环播放一个动作,后面的BUFFEND函数生效后怎么打断前面函数的动作

问题描述 LUA:前面的函数循环播放一个动作,后面的BUFFEND函数生效后怎么打断前面函数的动作 local y; function action(y) while y== 0 do player:doaction(""asdasd"") player:settime(falsehanshu1""1000) endend function hanshu1() local player:doaction(""asdasdad&

javascript-jsp中嵌入了js,我如何将js中定义的变量保存一个星期,就类似于计数器之类的变量

问题描述 jsp中嵌入了js,我如何将js中定义的变量保存一个星期,就类似于计数器之类的变量 我要怎么让我的三维数组temporary保存一个星期,并且值不会每次一刷新页面就重置,然后要把值传回给action层 解决方案 js中如何判断一个变量未定义js判断一个变量是否定义的方法 解决方案二: window.onload=function(){ var temporary = new Array(); for(var i=0;i<6;i++){ temporary[i] = new Array(