我在创建一个程序时遇到了麻烦,该程序将Array中的数字输入并通过字符串返回它们。我的
中的以下代码main.m文件:
#import <Foundation/Foundation.h>
#import "NumberRepeater.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NumberRepeater *repeater1 = [[NumberRepeater alloc]
initWithNumbers: @[@[@1, @10]]];
[repeater1 initWithNumbers:@[@[@1, @10]]];
NSLog(@"%@", [repeater1 process]);
NumberRepeater *repeater2 = [[NumberRepeater alloc]
initWithNumbers: @[@[@1, @2], @[@2, @3]]];
NSLog(@"%@", [repeater2 process]);
NumberRepeater *repeater3 = [[NumberRepeater alloc]
initWithNumbers: @[@[@10, @4], @[@34, @6], @[@92, @2]]];
NSLog(@"%@", [repeater3 process]);
}
return 0;
}
NumberRepeater.h文件中的代码:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NumberRepeater : NSObject
- (instancetype)initWithNumbers:(NSArray *)numbers;
- (NSString *)process;
@end
NS_ASSUME_NONNULL_END
NumberRepeater.m文件中的代码:
#import <Foundation/Foundation.h>
#import "NumberRepeater.h"
@implementation NumberRepeater
- (instancetype)initWithNumbers:(NSArray *)numbers {
NumberRepeater *repeater1 = [[NumberRepeater alloc]
initWithNumbers: @[@[@1, @10]]];
[repeater1 initWithNumbers:@[@[@1, @10]]];
NSLog(@"%@", repeater1);
return 0;
}
- (NSString *)process {
return 0;
}
@end
我的预期输出应为:
1111111111
11, 222
10101010, 343434343434, 9292
这绝不是我需要解决的学校作业。我希望有一个解决方案,但是对此的指导会很棒。我做完类似的问题并获得所需的输出后,就快要拥有它了,但显然我很想念,我只是不知道是什么。我了解我的处理方法缺少代码,只是我不完全知道该放在哪里。任何帮助将不胜感激!
答案 0 :(得分:2)
•我需要根据您的问题为您解释数组语法,
首先,您可以以这种方式@[@1,@2,....]
声明NSNumber
的数组
对象为Int
值,因为数组仅包含id对象,因此我们不能直接使用Int,而是使用NSnumber @ 1
数组内的对象可以是Array本身,因此我们可以声明数组对象的数组
像这个例子@[@[@10, @4], @[@3, @6], @[@2, @9]]
这样的数组数组
@[@[],@[],....etc]
•根据您的问题,我给您NSArray
两件
只需给我一个字符串,其中第一项重复(第二项)次
Example :
Input @[@1,@2] repeat number#1 two times
Output should be 11
Input @[@1,@10] repeat number#1 10 times
Output should be 1111111111
Input @[@[@1,@3],@[@3,@4]] repeat number#1 3 times , number#3 4 time
Output should be 111 3333
解决方案
NumberRepeater.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NumberRepeater : NSObject {
NSArray * numbers; // private number array will be initialized during initialization of object
}
- (instancetype)initWithNumbers:(NSArray *)numbers;
- (NSString *)process;
@end
NS_ASSUME_NONNULL_END
NumberRepeater.m
#import "NumberRepeater.h"
#import <Foundation/Foundation.h>
// NumberRepeater Extention for set private numbers
@interface NumberRepeater ()
@property (nonatomic,retain) NSArray *numbers;
@end
@implementation NumberRepeater
@synthesize numbers;
- (instancetype)initWithNumbers:(NSArray *)numbers {
self = [super init];
if (self) {
self.numbers = numbers;
}
return self;
}
- (NSString *)process {
// if numbers have No Data
if (numbers.count == 0) { return @"0"; }
// Array of array each array have two element first is number to repeat and second have number of repetation
// Example @[@1, @2] repeat number(1) TWO times so out put should be 11
// @[@[@1, @2], @[@2, @3]]
NSMutableString * output = [NSMutableString new];
for (NSArray*item in numbers) {
// Array should be with this Format
// Example @[@1, @2] repeat number(1) TWO times
// assert item have tow items only like that @[@1,@2]
NSAssert(item.count == 2,@"Array Should have Two items only");
NSNumber *numberToRepeat = item[0]; // repeat
NSNumber *repetitionCount = item[1]; //repetition Count
NSMutableString * repeatdString = [NSMutableString new];
for (NSInteger i= 0; i < [repetitionCount integerValue] ; i++) {
[repeatdString appendString:[numberToRepeat stringValue]];
}
[output appendString:repeatdString];
[output appendFormat:@" "]; // space on join with muti array
}
return output;
}
@end
main.m
#import <Foundation/Foundation.h>
#import "NumberRepeater.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NumberRepeater *repeater1 = [[NumberRepeater alloc]
initWithNumbers: @[@[@1, @10]]];
NSLog(@"%@", [repeater1 process]);
NumberRepeater *repeater2 = [[NumberRepeater alloc]
initWithNumbers: @[@[@1, @2], @[@2, @3]]];
NSLog(@"%@", [repeater2 process]);
NumberRepeater *repeater3 = [[NumberRepeater alloc]
initWithNumbers: @[@[@10, @4], @[@34, @6], @[@92, @2]]];
NSLog(@"%@", [repeater3 process]);
}
return 0;
}
答案 1 :(得分:0)
我无法正确理解您的问题,但我想您需要一个函数,该函数接受参数为数字的数组,并返回由所有数字组合而成的字符串,如果您想要的话,您可以创建这样的函数:->
-(NSString *)getStringFromArray:(NSArray <NSNumber *> *)array{
NSString *numberString = @"";
for (NSNumber *no in array) {
numberString = [numberString stringByAppendingString:no.stringValue];
}
return numberString;
}
像这样调用类中的方法:-
NSLog(@"Number String %@",[self getStringFromArray:@[@1,@2,@3,@4]]);
输出
Number String 1234