Ios 在数组中存储函数并将其应用于数字数组

Ios 在数组中存储函数并将其应用于数字数组,ios,objective-c,arrays,function,Ios,Objective C,Arrays,Function,我已经用Python为我的iOS游戏原型化了一个算法,我需要用ObjC重写。基本上,我有一个由16个数字组成的电路板,我想将每个数字循环三次,并使用四个函数(加、减、乘、幂)。1+2+3、2*3-4、3^4-5、9-4^3等,但没有操作顺序(第一次操作总是先完成) 我想要的是概述如何在Objective-C中实现这一点。具体来说,Objective-C中的函数数组的等价物是什么?有没有一种简单的方法可以用选择器实现它?带数字的循环的最佳结构是什么?NSInteger的数组s,int的数组s,NS

我已经用Python为我的iOS游戏原型化了一个算法,我需要用ObjC重写。基本上,我有一个由16个数字组成的电路板,我想将每个数字循环三次,并使用四个函数(加、减、乘、幂)。1+2+3、2*3-4、3^4-5、9-4^3等,但没有操作顺序(第一次操作总是先完成)

我想要的是概述如何在Objective-C中实现这一点。具体来说,Objective-C中的函数数组的等价物是什么?有没有一种简单的方法可以用选择器实现它?带数字的循环的最佳结构是什么?
NSInteger的数组
s,
int的数组
s,
NSArray
/
NSMutableArray
NSNumbers

import random as rand
min = 0
max = 9

max_target = 20

maximum_to_calculate = 100


def multiply(x, y):
    return x * y


def exponate(x, y):
    return x ** y


def add(x, y):
    return x + y


def subtract(x, y):
    return x - y

function_array = [multiply, exponate, add, subtract]

board = [rand.randint(min, max) for i in xrange(0, 16)]

dict_of_frequencies = {}

for a in board:
    for b in board:
        for first_fun in function_array:
            first_result = first_fun(a, b)
            for c in board:
                for second_fun in function_array:
                    final_result = second_fun(first_result, c)
                    if final_result not in dict_of_frequencies:
                        dict_of_frequencies[final_result] = 0
                    dict_of_frequencies[final_result] += 1

在Objective-C中,构造函数数组最方便的方法是使用块:

typedef NSInteger (^ArithmeticBlock)(NSInteger, NSInteger);

ArithmeticBlock add = ^NSInteger (NSInteger x, NSInteger y){
    return x + y;
};

ArithmeticBlock sub = ^NSInteger (NSInteger x, NSInteger y){
    return x - y;
};

NSArray * operations = @[add, sub];

由于需要在
NSNumber
s上执行算术运算,因此最好将电路板的值创建并存储为原语,例如
NSInteger
s,存储在普通的C数组中。您可以在以后很容易地将它们装箱,如果需要--
@(boardValue)
为您提供了一个
NSNumber

如果您想使用直接的C函数指针进行此操作,类似这样的操作可以:

#include <stdio.h>
#include <math.h>

long add(int a, int b) {
    return a + b;
}

long subtract(int a, int b) {
    return a - b;
}

long multiply(int a, int b) {
    return a * b;
}

long exponate(int a, int b) {
    return pow(a, b);
}

int main(void) {
    long (*mfunc[4])(int, int) = {add, subtract, multiply, exponate};
    char ops[4] = {'+', '-', '*', '^'};

    for ( int i = 0; i < 4; ++i ) {
        printf("5 %c 9 = %ld\n", ops[i], mfunc[i](5, 9));
    }

    return 0;
}

函数指针语法可能有点复杂
long(*mfunc[4])(int,int)
基本上可以转化为定义一个四元素数组,称为
mfunc
,该数组包含指向返回
long
并接受两个
int

类型的参数的函数的指针。无论如何,我会尝试一下,只是为了好玩。 这从未见过编译器。因此,请原谅我所有的打字错误和小语法错误提前

#include <stdlib.h>

...

const int MIN = 0;
const int MAX = 9;
const int MAX_TARGET = 20;
const int MAX_TO_CALCULATE = 100;

...

- (int) multiply:(int)x with:(int)y { return x * y; } 
- (int) exponate:(int)x with:(int)y { return x ^ y; } 
- (int) add:(int)x to:(int)y { return x + y; } 
- (int) substract:(int)x by:(int)y { return x - y; } 

// some method should start here, probably with 
-(void) someMethod {

NSArray *functionArray = [NSArray arrayWithObjects: @selector(multiply::), @selector(exponate::), @selector(add::), @substract(multiply::), nil]; // there are other ways of generating an array of objects

NSMutableArray *board = [NSMutableArray arrayWithCapacity:16]; //Again, there are other ways available.

for (int i = 0; i < 16; i++) {
  [board addObject:@(arc4random() % (MAX-MIN) + MIN)];
}

NSMutableDictionary dictOfFrequencies = [[NSMutableDictionary alloc] init];  

for (NSNumber a in board) 
  for (NSNumber b in board) 
    for (SEL firstFun in functionArray) {
      NSNumber firstResult = @([self performSelector:firstFun withObject:a withObject:b]);
      NSNumber countedResults = [dictOfFrequencies objectForKey:firstResult];
      if (countedResults) {
        [dictOfFrequencies removeObjectForKey:firstResult];
        countedResults = @(1 + [countedResults intValue]);
      } else {
        countedResults = @1; // BTW, using the @ followed by a numeric expression creates an NSNumber object with the value 1. 
      }
      [dictOfFrequencies setObject:countedResults forKey:firstResult];
    }
}
#包括
...
常数int MIN=0;
常数int MAX=9;
const int MAX_TARGET=20;
const int MAX_TO_CALCULATE=100;
...
-(int)将:(int)x与:(int)y{返回x*y;}
-(int)用:(int)y{return x^y;}
-(int)将:(int)x添加到:(int)y{返回x+y;}
-(int)减:(int)x乘:(int)y{返回x-y;}
//一些方法应该从这里开始,可能是
-(无效)某种方法{
NSArray*functionArray=[NSArray arrayWithObjects:@selector(multiply::),@selector(expontate::),@selector(add:),@substract(multiply::),nil];//还有其他生成对象数组的方法
NSMutableArray*board=[NSMutableArray阵列,容量:16];//同样,还有其他方法可用。
对于(int i=0;i<16;i++){
[board addObject:@(arc4random()%(最大值-最小值)+最小值)];
}
NSMutableDictionary dictOfFrequencies=[[NSMutableDictionary alloc]init];
用于(板中的NSA号)
用于(板中的NSB号)
用于(功能方面的第一乐趣){
NSNumber firstResult=@([自执行选择器:firstFun withObject:a withObject:b]);
NSNumber countedResults=[DictOfFrequences objectForKey:firstResult];
如果(计数结果){
[dictOfFrequencies removeObjectForKey:firstResult];
countedResults=@(1+[countedResults intValue]);
}否则{
countedResults=@1;//顺便说一句,使用@后跟数字表达式创建值为1的NSNumber对象。
}
[DictOfFrequences setObject:countedResults forKey:firstResult];
}
}
好吧,在其他人之前,让我先添加一些评论。:-) 不需要目标c。python代码是迭代的,因此可以用普通C实现。普通C在任何目标C都可用。
如果您真的想在这里使用Objective-C,那么您应该忘记python代码,并以OOP风格在Objective-C中实现相同的逻辑(目标是相同的结果)。我的代码真的试图尽可能接近地翻译您的代码。因此,我的代码既不是好的风格,也不是可维护的,也不是正确的OOP。在您思考之前,请记住这一点,ObjC与python相比是复杂的:-)

以下是一些随机想法:使用NS(可变)数组。不用担心C数组。您不能使用选择器调用普通的C函数,这些函数仅用于Obj-C方法。看看C函数指针。看看目标C块-谢谢,rmaddy指出了这一点,我对这个问题做了一些编辑。然而,我不想发布我的Objective-C代码,因为我没有具体的问题,它看起来过于臃肿和低效。我不想要一个精确的算法,我可以复制并粘贴到我的代码中,我只是想知道一个有经验的开发人员将如何做这样的事情;我希望Clang中的新ObjC文字内容将扩展为算术功能。这正是我想要的东西,我不知道选择器可以这样使用,谢谢!不客气。好吧,你可以通过接受答案来表达你的伟大。:-)您需要将选择器存储和检索为NSValues。存储方式类似于
[NSValue-valueWithPointer:@selector(yourSelector:)]并检索,如
SEL yourSelector=yourArray[0].pointerValue我会的,只需要到一台电脑上,不知道如何在我正在使用的应用程序上完成。你提到这不是一个真正的OOP方法,你介意给我一个非常简短的概述,我可以改变什么使它更OOP?一个NSArray可以拥有一个选择器吗?当我这样做的时候,它给了我一个错误。
#include <stdlib.h>

...

const int MIN = 0;
const int MAX = 9;
const int MAX_TARGET = 20;
const int MAX_TO_CALCULATE = 100;

...

- (int) multiply:(int)x with:(int)y { return x * y; } 
- (int) exponate:(int)x with:(int)y { return x ^ y; } 
- (int) add:(int)x to:(int)y { return x + y; } 
- (int) substract:(int)x by:(int)y { return x - y; } 

// some method should start here, probably with 
-(void) someMethod {

NSArray *functionArray = [NSArray arrayWithObjects: @selector(multiply::), @selector(exponate::), @selector(add::), @substract(multiply::), nil]; // there are other ways of generating an array of objects

NSMutableArray *board = [NSMutableArray arrayWithCapacity:16]; //Again, there are other ways available.

for (int i = 0; i < 16; i++) {
  [board addObject:@(arc4random() % (MAX-MIN) + MIN)];
}

NSMutableDictionary dictOfFrequencies = [[NSMutableDictionary alloc] init];  

for (NSNumber a in board) 
  for (NSNumber b in board) 
    for (SEL firstFun in functionArray) {
      NSNumber firstResult = @([self performSelector:firstFun withObject:a withObject:b]);
      NSNumber countedResults = [dictOfFrequencies objectForKey:firstResult];
      if (countedResults) {
        [dictOfFrequencies removeObjectForKey:firstResult];
        countedResults = @(1 + [countedResults intValue]);
      } else {
        countedResults = @1; // BTW, using the @ followed by a numeric expression creates an NSNumber object with the value 1. 
      }
      [dictOfFrequencies setObject:countedResults forKey:firstResult];
    }
}