我刚刚开始在MonoMac上开展一个项目,到目前为止这个项目非常酷.但是还有一些我不确定的事情.例如:你如何使用数组?这是我发现的:当我NSArray从一个我正在调用的方法返回时,我尝试获取该数组中的一个自定义对象,我不断得到类似" cannot convert type System.IntPtr to MyType"的东西.
NSArray groupArray = (NSArray)groupDictionary.ObjectForKey(key);
MyType myObject = (MyType)groupArray.ValueAt(0);
Run Code Online (Sandbox Code Playgroud)
这是我回来的数组.但是,如果我想自己创建一个数组怎么办?NSArray的实现不允许我实例化它.所以如果我把MonoMac网站弄好了,我应该使用像这样的普通数组
int[] intArray = int[10];
Run Code Online (Sandbox Code Playgroud)
分别是一个强类型数组,我不知道如何在C#中使用它.
那么去这里的方式是什么?
谢谢
-f
这是从NSArray中删除空字符串的正确且最有效的方法吗?
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myStrings = [NSMutableArray arrayWithObjects:(id[]){@"Test 1",@"",@"Test 2",@"Test 3",@""} count:5];
NSArray *myFilteredArray = [myStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]; //Non-destructive, myStrings is still intact
NSLog(@"The original array is: %@",myStrings);
NSLog(@"The filtered array is: %@",myFilteredArray);
[myStrings removeObject:@""]; //Destructive. myStrings will never be the same again
NSLog(@"The altered is: %@",myStrings);
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想根据他们的frame.origin.y对UIViews的NSMutableArray进行排序,我希望y的最低视图是第一个等等.可能是2个UIViews具有相同的起源.这有一个现有的方法吗?
我有一个NSArray包含一些NSString对象.例如:
NSArray *objects = @[@"Stin",@"Foo",@"Ray",@"Space"];
Run Code Online (Sandbox Code Playgroud)
现在我需要根据字符串的顺序对此数组进行排序.
NSArray *sortOrder = @[@"John",@"Foo",@"Space",@"Star",@"Ray",@"Stin"];
Run Code Online (Sandbox Code Playgroud)
所以答案应该是
NSArray *sorted = @[@"Foo",@"Space",@"Ray",@"Stin"];
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
答案: 基于对dasblinkenlight的接受答案,我做了以下,并且它有魅力.
NSMutableArray *objects = @[@"Star",@"Stin",@"Foo",@"Ray",@"Space",@"John"];
NSArray *sortOrder = @[@"John",@"Foo",@"Space",@"Star",@"Ray",@"Stin"];
[objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
int index1 = [sortOrder indexOfObject:obj1];
int index2 = [sortOrder indexOfObject:obj2];
if (index1 > index2)
return NSOrderedDescending;
if (index1 < index2)
return NSOrderedAscending;
return NSOrderedSame;
}];
Run Code Online (Sandbox Code Playgroud) 我试图用不同的颜色绘制UIBezierPath线是我的失败.所有行都更改为当前选定的颜色.我的所有路径和信息都存储在名为pathInfo的NSMutableArray中.在路径信息中,我放入包含路径,颜色,宽度和行类型的数组.除了所有线条都变成用户选择的任何颜色之外,这种方法都很好.我非常感谢任何帮助!
- (void)drawRect:(CGRect)rect {
UIBezierPath *drawPath = [UIBezierPath bezierPath];
drawPath.lineCapStyle = kCGLineCapRound;
drawPath.miterLimit = 0;
for (int i = 0; i < [pathInfo count]; i++){
NSArray *row = [[NSArray alloc] initWithArray:[pathInfo objectAtIndex:i]];
NSLog(@"Path: %@",[row objectAtIndex:0]);
NSLog(@"Color: %@",[row objectAtIndex:1]);
NSLog(@"Width: %@",[row objectAtIndex:2]);
NSLog(@"Type: %@",[row objectAtIndex:3]);
//width
drawPath.lineWidth = [[row objectAtIndex:2] floatValue];
//color
[[row objectAtIndex:1] setStroke];
//path
[drawPath appendPath:[row objectAtIndex:0]];
}
UIBezierPath *path = [self pathForCurrentLine];
if (path)
[drawPath appendPath:path];
[drawPath stroke];
}
- (UIBezierPath*)pathForCurrentLine {
if (CGPointEqualToPoint(startPoint, CGPointZero) && CGPointEqualToPoint(endPoint, CGPointZero)){ …Run Code Online (Sandbox Code Playgroud) 我有一种情况,我希望能够维护一个可能都指向nil的指针数组.
Equipment *equipment[19];
Run Code Online (Sandbox Code Playgroud)
但是,我发现我不能将指针数组或双指针设置为对象的属性.
当我不能使用C风格的数组时,我的解决方法是使用NSArray对象.所以我尝试做类似以下的事情:
NSMutableArray *equipment = [NSMutableArray arrayWithCapacity: NUM_EQUIPSLOTS];
for (int i=0; i<NUM_EQUIPSLOTS; i++) {
[equipment setObject: nil atIndexedSubscript: i];
}
Run Code Online (Sandbox Code Playgroud)
这里的想法是我有一个空指针数组,稍后会指向东西.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x246e012 0x1e4be7e 0x2421b6a 0x2421a20 0xebd02 0xde82b 0xc9d5a 0xcb6bd 0x4d525 0xc94fa 0xc8b6a 0xa18157 0xa18747 0xa1994b 0xa2acb5 0xa2bbeb 0xa1d698 0x3176df9 0x3176ad0 0x23e3bf5 0x23e3962 0x2414bb6 0x2413f44 0x2413e1b 0xa1917a 0xa1affc 0xc8526 0x1fa5)
libc++abi.dylib: terminate called throwing an exception
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用C风格的数组和单个对象轻松地完成这项工作.我宁愿这样做而不是像哑巴一样: …
我需要对NSArray包含时间NSString的内容进行排序,例如:
NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil];
Run Code Online (Sandbox Code Playgroud)
我需要的是按时间的升序对数组进行排序.
有没有办法做这样的事情?
我有一个方法我将'id'传递到这里:
NSString *id = @"4bf58dd8d48988d181941735";
[self getNames:id];
Run Code Online (Sandbox Code Playgroud)
这工作正常,但是当我使用segue中传递的对象的'id'时:
NSString *id = _selectedStore._id;
[self getNames:id];
Run Code Online (Sandbox Code Playgroud)
我收到错误:
'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
这是它得到错误的地方.我的尝试(记住我是新手)是取一个Foursquare id的数组,得到名字,然后得到idUrl的id.如果我删除所有创建photoUrl的代码(我已在代码中注明),它会运行而不会崩溃.
- (void)getNames {
NSMutableArray *final = [[NSMutableArray alloc] init];
for (SearchVenue *object in self.venues) {
[Foursquare2 venueGetDetail:object._id callback:^(BOOL success, id result){
if (success) {
NSDictionary *dic = result;
NSArray *venue = [dic valueForKeyPath:@"response.venue"];
self.venueDetail = venue;
NSMutableDictionary *newVen = [NSMutableDictionary dictionary];
[newVen setValue:[self.venueDetail valueForKey:@"name"] forKey:@"name"];
[final addObject:newVen];
[Foursquare2 venueGetPhotos:object._id limit:nil …Run Code Online (Sandbox Code Playgroud) 我似乎无法覆盖以下方法,并创建了一个独立的示例,现在让我烦恼了几个小时.
它几乎肯定是愚蠢的,但我似乎无法覆盖swift类中的类方法,其中基类是Objective C,特别是返回类型是一个NSArray*.
我刚收到错误," Method does not override any method from its superclass"
所以我的失败如下所示:
但是如果将返回类型更改为像NSString这样简单的东西,它可以正常工作*
现在我已经Array<AnyObject>和其他几个人一起尝试过了,但是我对Swift语法仍然有点新鲜,所以我几乎肯定错过了一些明显的东西.
我有一个公式重新调整数组作为例子var Array = [a,s,d,s,f,g,g,h,e].我想要的是运行一个for循环或其他一些让我回来的选项a,s,d,f,g,h,e- 只有唯一的值.我怎么能用ios Swift做到这一点?