将NSString的NSArray转换为NSNumber的NSArray的更好方法

时间:2015-06-25 09:37:18

标签: ios objective-c nsstring nsarray nsnumber

是否有更好的方法可将NSArray NSString NSArray转换为NSNumber -(NSArray*) convertStringArrayToNumberArray:(NSArray *)strings { NSMutableArray *numbers = [[NSMutableArray alloc] initWithCapacity:strings.count]; for (NSString *string in strings) { [numbers addObject:[NSNumber numberWithInteger:[string integerValue]]]; } return numbers; } ,而不是以下代码:

describe('Myctrl', function() {
  var $httpBackend, $scope, authRequestHandler, createController, url;
  $httpBackend = void 0;
  $scope = void 0;
  createController = void 0;
  authRequestHandler = void 0;
  url = void 0;
  beforeEach(module('myApp'));
  alert('Hello there!');
  beforeEach(inject(function($injector, $controller, $rootscope) {
    var controllerService;
    alert('Hello there!');
    $httpBackend = $injector.get('$httpBackend');
    alert('Hello there!');
    url = 'http://www.w3schools.com/angular/customers.php';
    $scope = $rootscope.$new();
    controllerService = $controller;
    createController = function() {
      return controllerService('Myctrl', {
        '$scope': $scope
      });
    };
  }));

  /*
    afterEach(function () {
      $httpBackend.verifyNoOutstandingExpectation();
      $httpBackend.verifyNoOutstandingRequest();
    });
   */
  it('should fetch authentication token', function() {
    $httpBackend.whenGET(url).respond();
    $httpBackend.expectGET(url).respond();
    $httpBackend.flush();
    createController();
    expect($scope.names).toBeTruthy(true);
  });
});

1 个答案:

答案 0 :(得分:13)

-(NSArray *)convertStringArrayToNumberArray:(NSArray *)strings {
    return [strings valueForKeyPath:@"self.integerValue"];
}

我测试

NSArray * array = @[@"1",@"2",@"3"];
NSArray *num = [self convertStringArrayToNumberArray:array];

结果