English 中文(简体)
从国家安全局获得物体
原标题:Getting an object from an NSSet

您可以拿物体 AtIndex: from an NSSet when how You do?

最佳回答

有几例使用案例。 http://www.un.org/Depts/DGACM/index_chinese.htm

如果你不想要重复,不关心秩序,想要快速成员测试,则一套措施是适当的。

问题回答

缩略语 AtIndex:

要求归还所有物体的NSArray的所有物体。

可使用过滤器 如果你有某种独特的识别标志来选择你所需要的物体,则要作解释。

第一,确定前提(假定你在物体上独一无二的宽度为“识别剂”,并且是一种国家动态):

NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"identifier == %@", identifier];

然后使用下列前提选择物体:

NSObject *myChosenObject = [mySet filteredSetUsingPredicate:myPredicate].anyObject;

NSArray *myArray = [myNSSet allObjects];

MyObject *object = [myArray objectAtIndex:(NSUInteger *)]

将NSUInteger改为你想要的物体索引。

用于:

//your current set
let mySet : NSSet
//targetted index
let index : Int
//get object in set at index
let object = mySet.allObjects[index]

国家抽样调查采用这一方法是为了确定物体是否在其中。

因此,例如,如果你有一个数据模型,以粗略价值界定其独特性(即财产为:

@property NSUInteger objectID;

之后,你执行就是:

- (BOOL)isEqual:(id)object
{
  return (self.objectID == [object objectID]);
}

并且你可以实施洗衣:

- (NSUInteger)hash
{
 return self.objectID;  // to be honest, I just do what Apple tells me to here
                       // because I ve forgotten how Sets are implemented under the hood
}

然后,你可以拿到该身份证的物体(并检查该身份证是否在安全局内)。

MyObject *testObject = [[MyObject alloc] init];
testObject.objectID = 5; // for example.  

// I presume your object has more properties which you don t need to set here 
// because it s objectID that defines uniqueness (see isEqual: above)

MyObject *existingObject = [mySet member: testObject];

// now you ve either got it or existingObject is nil

但是,是摆脱国家安全局的唯一办法,是首先考虑它的独特性。

我先试了什么速度越快,但我却避免使用点数,因为这可能是线性的,而使用该成员:方法会更快。 这是更喜欢使用NSSet而不是NSArray的原因之一。

for (id currentElement in mySet)
{
    // ** some actions with currentElement 
}

大部分时间,你不注意从一组物体中找到一个特定物体。 你们关心检测,看看一包含有物体。 这套东西是好的。 当你想看到某一物体是否属于集邮组时,其速度要快于阵列。

如果你不关心, 你们得到的反对使用-anyObject,这只是给你一个物体,例如把你的手放在一个袋子里,gra取一些东西。

Dog *aDog = [dogs anyObject]; // dogs is an NSSet of Dog objects

如果你关注什么物体,则使用<代码>---------------------------------------------------------------------------------- 你需要把目标摆在你面前。

Dog *spot = [Dog dogWithName:@"Spot"];
// ...
Dog *aDog = [dogs member:spot]; // Returns the same object as above

在这里,你可以在X条码中操作一些密码,以了解更多的情况。

NSString *one = @"One";
NSString *two = @"Two";
NSString *three = @"Three";

NSSet *set = [NSSet setWithObjects:one, two, three, nil];

// Can t use Objective-C literals to create a set.
// Incompatible pointer types initializing  NSSet *  with an expression of type  NSArray * 
//  NSSet *set = @[one, two, three];

NSLog(@"Set: %@", set);
// Prints looking just like an array but is actually not in any order
//Set: {(
//     One,
//     Two,
//     Three
//     )}

// Get a random object
NSString *random = [set anyObject];
NSLog(@"Random: %@", random); // Random: One

// Iterate through objects. Again, although it prints in order, the order is a lie
for (NSString *aString in set) {
    NSLog(@"A String: %@", aString);
}

// Get an array from the set
NSArray *array = [set allObjects];
NSLog(@"Array: %@", array);

// Check for an object
if ([set containsObject:two]) {
    NSLog(@"Set contains two");
}

// Check whether a set contains an object and return that object if it does (nil if not)
NSString *aTwo = [set member:two];
if (aTwo) {
    NSLog(@"Set contains: %@", aTwo);
}




相关问题
How do you create UIBarButtonItems with a radio interface?

I have a UIToolbar that needs three buttons in a radio style, meaning that of the three, only one button can be pushed at a time. The documentation makes reference to the possibility of setting up ...

iPhone settings bundle

I want to allow the user to enter a valid date using the iPhone’s settings application. I have experimented with many of the PreferenceSpecifiers data node types including date. I have two issues: ...

Circular #import/@class problem in ObjectiveC

I m going to use an example to properly illustrate my confusion. I can t quite wrap my head around this. In Cocoa touch, we have UIViewController and its subclass, UINavigationController. Now, UIVC ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Cocoa-Touch: issue looping MPMoviePlayerController

I have an app which has to load some data at startup, so I want to display a splash-screen animation. I m using the MPMoviePlayerController to play a m4v file. The movie has it s background set to [...

Iphone sequential animation with setAnimationDelay

I m trying to chain animation events. The application I m coding for work has a multiple choice quiz. First you pick your multiple choice answer. The quiz view fades away. Then a label ("correct" or "...

热门标签