用于将值添加到数组,然后将结果与 iPhone 上的另一个数组进行比较的按钮
我有十个按钮,每个按钮对应不同的数字。我希望记录按下这些按钮的顺序并将它们输入到一个数组中,然后将其与应用程序中的另一个静态数组进行比较。检查应在第 N 次按下按钮时完成,其中 N 等于另一个静态数组中的项目数。
如何 IA) 让按下按钮向数组添加一个值 B) 在按下第 N 个按钮后停止,该按钮等于我正在检查的数组中的数字数量 C) 将此数组与另一个数组进行比较?
I have ten buttons that each correspond to a different number. I'm looking to record the order that these buttons are pressed and enter them into an array and then compare it to another array that is static in the app. The check should be done on the Nth button press, where N equals the number of items in the other, static array.
How do I A) have a button press add a value to an array B) stop after Nth button press equal to amount of numbers in the array I'm checking against and C) compare this array to the other array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以:
决定 N 并创建一个数组,其中 N 个数字作为 NSNumber 对象(NSArray 和 NSMutableArray 需要对象)。
将按钮链接到操作。
在操作方法中,检查每个按钮的第一个参数以确定按下了哪个数字。
将该数字添加到可变数组。
当数组的大小等于 N 时,进行检查:使用从 0 到 N-1 的 for 循环,并从每个数组中获取该索引处的对象。使用 isEqual: 来比较它们。如果任何比较失败,那么您就知道数组不匹配。否则,你就成功了。
You could:
Decide on N and create an array with your N numbers as NSNumber objects (NSArray and NSMutableArray require objects).
Link the buttons to an action.
In the action method, check the first parameter against each button to determine which number was pressed.
Add that number to a mutable array.
When the array's size is equal to N, do your check: use a for loop from 0 to N-1 and get the objects at that index from each array. Compare them using
isEqual:
. If any comparison fails, then you know that the arrays don't match. Otherwise, you have success.