标签: nsmutablearray

将浮点值赋给NSMutableArray

我想在for循环中将浮点变量赋给nsmutable数组.我是这样做的.但是,nsmutable数组似乎为null.我怎么解决这个问题?(距离是浮点数,enyakinarray是NSMutablearray.)

for (int i=0; i<[ws3.CustomerID count]; i++) {

    //radian hesaplamas?
    float total = [first floatValue];
    float theta = total * M_PI/180;
    float total2 = [second floatValue];
    float theta2 = total2 * M_PI/180;
    float total3 = [[ws3.Latitude objectAtIndex: i]  floatValue];
    float theta3 = total3 * M_PI/180;
    float total4 = [[ws3.Longitude objectAtIndex: i] floatValue];
    float theta4 = total4 * M_PI/180;


     distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin( theta3)) ;

    NSLog(@"xxxxx %f",distance);

    num = [NSNumber …
Run Code Online (Sandbox Code Playgroud)

floating-point xcode objective-c nsmutablearray ios

2
推荐指数
1
解决办法
6250
查看次数

可变数组中的NSNumber = 0

如果我NSNumber在NSMutableArray遗嘱中存储等于0并且会被解释为nil并导致count方法出现问题?

null objective-c nsnumber nsmutablearray

2
推荐指数
1
解决办法
1178
查看次数

如何使用nsarray初始化nsmutablearray

我正在尝试NSMutableArray使用a的值初始化a NSArray但是当我NSLog新创建的NSMutableArray所有我得到的是(null),(null)...等

我不知道为什么会发生这种超级令人沮丧的事情.

我myMutableArray在标题@synthesize等设置..

然后在parserdidend委托中这是我尝试做的.

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    if (dataSetToParse == @"ggf"){        
        //Filter results (ISTOY = T)
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"ISTOY",@"T"];
        NSArray *filteredArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];

       NSLog(@"%@", filteredArray); //This prints correct details

        [myMutableArray setArray:filteredArray];

         NSLog(@"%@", myMutableArray); //This prints (null), (null)... etc

    //Use myMutableArray later...
    }
Run Code Online (Sandbox Code Playgroud)

这几乎总结了它.

iphone foundation nsmutablearray nsarray ios

2
推荐指数
1
解决办法
3577
查看次数

从NSMutableArray中删除对象

我试图删除索引1处的对象,但代码将无法编译.

我也不明白这一点:我将"iphone"字符串设置为索引0,然后将其从索引0中删除,但输出仍然首先显示"iphone".任何人都可以向我解释一下吗?

int main (int argc, const char * argv[])
{    
    @autoreleasepool {

        //create three string objetc
        NSString *banana  = @"This is banana";
        NSString *apple = @"This is apple";
        NSString *iphone =@"This is iPhone";

        //create  an empty array
        NSMutableArray *itemList = [NSMutableArray array];

        // add the item to the array
        [itemList addObject:banana];
        [itemList addObject:apple];

        // put the iphone to the at first

        [itemList insertObject:iphone atIndex:0];

        for (NSString *l in itemList) {
            NSLog(@"The  Item in the list is %@",l);
        }
        [itemList …
Run Code Online (Sandbox Code Playgroud)

objective-c nsmutablearray

2
推荐指数
1
解决办法
8490
查看次数

对2D NSMutableArray进行排序

我有2D数组如下:

NSMutableArray *world=[[NSMutableArray alloc]init];
NSNumber *number;
NSMutableArray *inner;

int scr=2;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"ABC",number,nil];
[world addObject:inner];

scr=6;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"XYZ",number,nil];
[world addObject:inner];

scr=1;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"PQR",number,nil];
[world addObject:inner];

scr=5;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"DEF",number,nil];
[world addObject:inner];

scr=3;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"LMN",number,nil];
[world addObject:inner];
Run Code Online (Sandbox Code Playgroud)

现在,我想对world数组进行排序以获得以下结果,

XYZ 6
DEF 5
LMN 3
ABC 2
PQR 1
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我,提前谢谢...

sorting iphone objective-c nsmutablearray ios

2
推荐指数
1
解决办法
534
查看次数

目标C - 矩阵乘法慢性能

我有2个2-D NSMutableArrays,我正在尝试做一些基本的矩阵乘法.我的通用公式代码如下,但其性能异常缓慢(正如预期的那样).我已经做了很多谷歌搜索,并没有找到任何简单易懂的公式来改变性能增强的代码.任何人都可以指出我在一个简单的公式/教程/示例的正确方向,如何在目标C中使用矩阵乘法获得比0(n ^ 3)更好的性能.

+ (NSMutableArray*)multiply:(NSMutableArray*)a1 withArray:(NSMutableArray*)a2
{
    if([[a1 objectAtIndex: 0] count] != [a2 count])
    {
        NSLog(@"Multiplicaton error!");
        return NULL;
    }

    int a1_rowNum = [a1 count];
    int a2_rowNum = [a2 count];
    int a2_colNum = [[a2 objectAtIndex:0] count];
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:a1_rowNum];
    for (int i = 0; i < a1_rowNum; i++) {
        NSMutableArray *tempRow = [NSMutableArray arrayWithCapacity:a2_colNum];
        for (int j = 0; j < a2_colNum; j++) {
            double tempTotal = 0;
            for (int k = 0; k < a2_rowNum; k++) …
Run Code Online (Sandbox Code Playgroud)

objective-c matrix multiplication nsmutablearray matrix-multiplication

2
推荐指数
1
解决办法
1340
查看次数

将字符串对象添加到NSMutuableArray

我正在尝试将NSString对象添加到NSMutuableArray : tableItems = [tableItems addObject:timeString];. 我已经初始化了数组但是当我添加NSString对象时,我收到此错误:Assigning to 'NSMutuableArray *_strong'from incompatible type void.

整个代码在这里:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    UILabel *lbl;
    NSTimer *stopTimer;
    NSDate *startDate;
    BOOL running,lap;
    UIButton *bttn;
    NSMutableArray *tableItems;
    NSString *timeString;
}

@property (strong,nonatomic) IBOutlet UILabel *lbl;
@property (strong,nonatomic) IBOutlet UIButton *bttn;
@property (strong,nonatomic) NSMutableArray *tableItems;
@property (strong,nonatomic) NSString *timeString;

-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;

-(void)updateTimer;

@end
Run Code Online (Sandbox Code Playgroud)

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize lbl,bttn,tableItems,timeString;

- (void)viewDidLoad
{
    [super viewDidLoad];
    lbl.text = @"00.00.00.000";
    running …
Run Code Online (Sandbox Code Playgroud)

objective-c nsmutablearray ios

2
推荐指数
1
解决办法
188
查看次数

生成随机字母串的有效方法?

我想要随机化字母表中所有字符的字符串.现在,我创建了一个包含26个字符的可变数组,使用exchangeObjectAtIndex:方法对它们进行随机播放,然后将每个字符添加到我返回的字符串中.

必须有更好的方法来做到这一点.这是我的代码:

- (NSString *)shuffledAlphabet {
    NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]];

    for (NSUInteger i = 0; i < [shuffledAlphabet count]; ++i) {
        // Select a random element between i and end of array to swap with.
        int nElements = [shuffledAlphabet count] - i;
        int n = (random() % nElements) + i;
        [shuffledAlphabet exchangeObjectAtIndex:i withObjectAtIndex:n];
    }

    NSString *string = [[NSString alloc] init];
    for (NSString *letter in shuffledAlphabet) {
        string = [NSString stringWithFormat:@"%@%@",string,letter];
    }

    return string;
}
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c nsstring nsmutablearray ios

2
推荐指数
1
解决办法
2799
查看次数

如何在文本视图中的NSMutableArray中附加值?

我有一个在self中有很多名字的数组.这是我的代码:

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"figo",@"messi",@"totti",@"ronaldo",@"pirlo", nil];
Run Code Online (Sandbox Code Playgroud)

我想在一个TextView中附加这个名字,任何名字都在一行......但是当我最后运行这个应用程序时,我看到姓氏在UITextView中,另一个名字被删除!请指导我.

这是我的完整代码:

ViewController.h

@interface ViewController : UIViewController
{
    IBOutlet UITextView *text;
}
@property(nonatomic,strong) IBOutlet UITextView *text;

@end
Run Code Online (Sandbox Code Playgroud)

ViewController.m

@implementation ViewController
@synthesize text;

- (void)viewDidLoad
{
    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"figo",@"messi",@"totti",@"ronaldo",@"pirlo", nil];
    [super viewDidLoad];
    for (int i=0; i < [array count]; i++) {
        NSString *a = [array objectAtIndex:i];
        NSLog(@"%@",a);
        [text setText:a];
    }
}
@end
Run Code Online (Sandbox Code Playgroud)

我希望这个名字附加在UITextView中,这些名称在一行......就像这样:

figo
messi
totti
ronaldo
pirlo 
Run Code Online (Sandbox Code Playgroud)

objective-c uitextview nsmutablearray ios

2
推荐指数
1
解决办法
1541
查看次数

在特定索引处将NSMutableDictionary添加到NSMutableArray?

我试图在特定索引上将NSMutableDictionary添加到NSMutableArray中

NSMutableArray已填充数据来自webservice,但我想要

在0索引上添加我的数组中的另一个字典.

但我有点困在这里:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: ' -[JKArray insertObject:atIndex:]: mutating method sent to immutable object'
Run Code Online (Sandbox Code Playgroud)

我的代码是:

在.h文件中

@property (strong, nonatomic)NSMutableArray *cityArray;

@property (strong, nonatomic)NSMutableDictionary *cityDict;

.m file:

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.cityArray = [[NSMutableArray alloc]init];

self.cityDict  = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"All Cities",@"key1", nil];

    NSLog(@"City Dict=> %@", cityDict);
}


-(void)ConnectionDidFinishLoading:(NSString *)responseString :(NSString *)serviceName{

        self.dealsDict = [responseString objectFromJSONString];

        self.cityArray = [self.dealsDict objectForKey:@"Cities"];

        [self.cityArray addObject:[self.cityDict copy]];

        [self.cityArray addObject:self.cityDict];

        [self.cityArray objectAtIndex:0];

        [self.tableView reloadData];

}
Run Code Online (Sandbox Code Playgroud)

请帮忙

谢谢.

objective-c nsmutablearray nsmutabledictionary

2
推荐指数
1
解决办法
1158
查看次数