How to create ranges from NSArray by filtering matching conditions?












-1















NSArray *objects = @[object1, object2, object3, object1, object5, object1, object6, object7];


How would be best to get the ranges delimited by object1 like this:



(0-2 3-4 5-7)


I know of this method to create a NSIndexSet from an array, but im not sure how i could use it to achieve the above index set.



NSIndexSet*indexSet = [objects indexesOfObjectsPassingTest:^(id object, NSUInteger idx, BOOL *stop) {
return ; //Do some checks
}];


Using objects.count may also be useful for the final range, but I am unsure of the best way of doing this.










share|improve this question




















  • 2





    Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think an NSIndexSet is what you want. You want an array of NSRange. You would just need to iterate over the array, building the ranges.

    – Paulw11
    Nov 14 '18 at 1:28











  • For an index set there's not really a distinction between (0-2 3-4 5-7) and (0-7). An index is either in the set or not. There's no "grouping".

    – Ken Thomases
    Nov 14 '18 at 1:37











  • @Paulw11 Youre correct. Ive fixed the question body and altered the question to allow other possible solutions.

    – Kane Buckthorpe
    Nov 14 '18 at 1:37













  • @KaneBuckthorpe you've edited out the fact that you want the ranges to be delimited by object1.

    – Ken Thomases
    Nov 14 '18 at 1:40











  • @KenThomases There actually is some grouping as rangeAtIndex can be used on an NSIndexSet to return the range for the NSIndexSet index

    – Kane Buckthorpe
    Nov 14 '18 at 1:40


















-1















NSArray *objects = @[object1, object2, object3, object1, object5, object1, object6, object7];


How would be best to get the ranges delimited by object1 like this:



(0-2 3-4 5-7)


I know of this method to create a NSIndexSet from an array, but im not sure how i could use it to achieve the above index set.



NSIndexSet*indexSet = [objects indexesOfObjectsPassingTest:^(id object, NSUInteger idx, BOOL *stop) {
return ; //Do some checks
}];


Using objects.count may also be useful for the final range, but I am unsure of the best way of doing this.










share|improve this question




















  • 2





    Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think an NSIndexSet is what you want. You want an array of NSRange. You would just need to iterate over the array, building the ranges.

    – Paulw11
    Nov 14 '18 at 1:28











  • For an index set there's not really a distinction between (0-2 3-4 5-7) and (0-7). An index is either in the set or not. There's no "grouping".

    – Ken Thomases
    Nov 14 '18 at 1:37











  • @Paulw11 Youre correct. Ive fixed the question body and altered the question to allow other possible solutions.

    – Kane Buckthorpe
    Nov 14 '18 at 1:37













  • @KaneBuckthorpe you've edited out the fact that you want the ranges to be delimited by object1.

    – Ken Thomases
    Nov 14 '18 at 1:40











  • @KenThomases There actually is some grouping as rangeAtIndex can be used on an NSIndexSet to return the range for the NSIndexSet index

    – Kane Buckthorpe
    Nov 14 '18 at 1:40
















-1












-1








-1








NSArray *objects = @[object1, object2, object3, object1, object5, object1, object6, object7];


How would be best to get the ranges delimited by object1 like this:



(0-2 3-4 5-7)


I know of this method to create a NSIndexSet from an array, but im not sure how i could use it to achieve the above index set.



NSIndexSet*indexSet = [objects indexesOfObjectsPassingTest:^(id object, NSUInteger idx, BOOL *stop) {
return ; //Do some checks
}];


Using objects.count may also be useful for the final range, but I am unsure of the best way of doing this.










share|improve this question
















NSArray *objects = @[object1, object2, object3, object1, object5, object1, object6, object7];


How would be best to get the ranges delimited by object1 like this:



(0-2 3-4 5-7)


I know of this method to create a NSIndexSet from an array, but im not sure how i could use it to achieve the above index set.



NSIndexSet*indexSet = [objects indexesOfObjectsPassingTest:^(id object, NSUInteger idx, BOOL *stop) {
return ; //Do some checks
}];


Using objects.count may also be useful for the final range, but I am unsure of the best way of doing this.







ios objective-c nsarray






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 1:55







Kane Buckthorpe

















asked Nov 14 '18 at 0:55









Kane BuckthorpeKane Buckthorpe

157




157








  • 2





    Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think an NSIndexSet is what you want. You want an array of NSRange. You would just need to iterate over the array, building the ranges.

    – Paulw11
    Nov 14 '18 at 1:28











  • For an index set there's not really a distinction between (0-2 3-4 5-7) and (0-7). An index is either in the set or not. There's no "grouping".

    – Ken Thomases
    Nov 14 '18 at 1:37











  • @Paulw11 Youre correct. Ive fixed the question body and altered the question to allow other possible solutions.

    – Kane Buckthorpe
    Nov 14 '18 at 1:37













  • @KaneBuckthorpe you've edited out the fact that you want the ranges to be delimited by object1.

    – Ken Thomases
    Nov 14 '18 at 1:40











  • @KenThomases There actually is some grouping as rangeAtIndex can be used on an NSIndexSet to return the range for the NSIndexSet index

    – Kane Buckthorpe
    Nov 14 '18 at 1:40
















  • 2





    Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think an NSIndexSet is what you want. You want an array of NSRange. You would just need to iterate over the array, building the ranges.

    – Paulw11
    Nov 14 '18 at 1:28











  • For an index set there's not really a distinction between (0-2 3-4 5-7) and (0-7). An index is either in the set or not. There's no "grouping".

    – Ken Thomases
    Nov 14 '18 at 1:37











  • @Paulw11 Youre correct. Ive fixed the question body and altered the question to allow other possible solutions.

    – Kane Buckthorpe
    Nov 14 '18 at 1:37













  • @KaneBuckthorpe you've edited out the fact that you want the ranges to be delimited by object1.

    – Ken Thomases
    Nov 14 '18 at 1:40











  • @KenThomases There actually is some grouping as rangeAtIndex can be used on an NSIndexSet to return the range for the NSIndexSet index

    – Kane Buckthorpe
    Nov 14 '18 at 1:40










2




2





Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think an NSIndexSet is what you want. You want an array of NSRange. You would just need to iterate over the array, building the ranges.

– Paulw11
Nov 14 '18 at 1:28





Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think an NSIndexSet is what you want. You want an array of NSRange. You would just need to iterate over the array, building the ranges.

– Paulw11
Nov 14 '18 at 1:28













For an index set there's not really a distinction between (0-2 3-4 5-7) and (0-7). An index is either in the set or not. There's no "grouping".

– Ken Thomases
Nov 14 '18 at 1:37





For an index set there's not really a distinction between (0-2 3-4 5-7) and (0-7). An index is either in the set or not. There's no "grouping".

– Ken Thomases
Nov 14 '18 at 1:37













@Paulw11 Youre correct. Ive fixed the question body and altered the question to allow other possible solutions.

– Kane Buckthorpe
Nov 14 '18 at 1:37







@Paulw11 Youre correct. Ive fixed the question body and altered the question to allow other possible solutions.

– Kane Buckthorpe
Nov 14 '18 at 1:37















@KaneBuckthorpe you've edited out the fact that you want the ranges to be delimited by object1.

– Ken Thomases
Nov 14 '18 at 1:40





@KaneBuckthorpe you've edited out the fact that you want the ranges to be delimited by object1.

– Ken Thomases
Nov 14 '18 at 1:40













@KenThomases There actually is some grouping as rangeAtIndex can be used on an NSIndexSet to return the range for the NSIndexSet index

– Kane Buckthorpe
Nov 14 '18 at 1:40







@KenThomases There actually is some grouping as rangeAtIndex can be used on an NSIndexSet to return the range for the NSIndexSet index

– Kane Buckthorpe
Nov 14 '18 at 1:40














1 Answer
1






active

oldest

votes


















0














An NSIndexSet won't really help, since you are after ranges, not indices.



You could simply iterate over the array looking for your sentinel object object1, adding instances of NSRange to an array.



Except, that NSRange is a struct, so you can't add instances directly to NSMutableArray. There are a few options:




  1. (And this would be my first choice): Use Swift instead of Objective-C

  2. Create a custom object to wrap NSRange

  3. Use NSValue to wrap NSRange

  4. Use a C array rather than NSArray


I am going to use option 2, since option 3 involves messing around with the raw bytes of the underlying struct.



Range.h



@interface Range : NSObject

@property (readonly) NSRange range;

- (id) initWithRange: (NSRange) range
@end


Range.m



import "Range.h"



@implementation Range



- (id) initWithRange: (NSRange) range {
if (self = [super init]) {
_range = range;
}

return self;
}

@end


YourCode.m



-(NSArray *) getRanges:(NSArray *)objects delimeter: (NSObject *)object1 
{
int rangeStart = -1;
NSMutableArray *ranges = [[NSMutableArray alloc]init];
int index;

for (index=0; index<objects.count;index++) {
if (objects[index] == object1) {
if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
rangeStart = index;
}
}

if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
return ranges;
}





share|improve this answer


























  • NSRange is not an object. It can't be added to an NSArray. You could wrap it in an NSValue, but that's cumbersome.

    – Ken Thomases
    Nov 16 '18 at 5:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291656%2fhow-to-create-ranges-from-nsarray-by-filtering-matching-conditions%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














An NSIndexSet won't really help, since you are after ranges, not indices.



You could simply iterate over the array looking for your sentinel object object1, adding instances of NSRange to an array.



Except, that NSRange is a struct, so you can't add instances directly to NSMutableArray. There are a few options:




  1. (And this would be my first choice): Use Swift instead of Objective-C

  2. Create a custom object to wrap NSRange

  3. Use NSValue to wrap NSRange

  4. Use a C array rather than NSArray


I am going to use option 2, since option 3 involves messing around with the raw bytes of the underlying struct.



Range.h



@interface Range : NSObject

@property (readonly) NSRange range;

- (id) initWithRange: (NSRange) range
@end


Range.m



import "Range.h"



@implementation Range



- (id) initWithRange: (NSRange) range {
if (self = [super init]) {
_range = range;
}

return self;
}

@end


YourCode.m



-(NSArray *) getRanges:(NSArray *)objects delimeter: (NSObject *)object1 
{
int rangeStart = -1;
NSMutableArray *ranges = [[NSMutableArray alloc]init];
int index;

for (index=0; index<objects.count;index++) {
if (objects[index] == object1) {
if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
rangeStart = index;
}
}

if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
return ranges;
}





share|improve this answer


























  • NSRange is not an object. It can't be added to an NSArray. You could wrap it in an NSValue, but that's cumbersome.

    – Ken Thomases
    Nov 16 '18 at 5:41
















0














An NSIndexSet won't really help, since you are after ranges, not indices.



You could simply iterate over the array looking for your sentinel object object1, adding instances of NSRange to an array.



Except, that NSRange is a struct, so you can't add instances directly to NSMutableArray. There are a few options:




  1. (And this would be my first choice): Use Swift instead of Objective-C

  2. Create a custom object to wrap NSRange

  3. Use NSValue to wrap NSRange

  4. Use a C array rather than NSArray


I am going to use option 2, since option 3 involves messing around with the raw bytes of the underlying struct.



Range.h



@interface Range : NSObject

@property (readonly) NSRange range;

- (id) initWithRange: (NSRange) range
@end


Range.m



import "Range.h"



@implementation Range



- (id) initWithRange: (NSRange) range {
if (self = [super init]) {
_range = range;
}

return self;
}

@end


YourCode.m



-(NSArray *) getRanges:(NSArray *)objects delimeter: (NSObject *)object1 
{
int rangeStart = -1;
NSMutableArray *ranges = [[NSMutableArray alloc]init];
int index;

for (index=0; index<objects.count;index++) {
if (objects[index] == object1) {
if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
rangeStart = index;
}
}

if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
return ranges;
}





share|improve this answer


























  • NSRange is not an object. It can't be added to an NSArray. You could wrap it in an NSValue, but that's cumbersome.

    – Ken Thomases
    Nov 16 '18 at 5:41














0












0








0







An NSIndexSet won't really help, since you are after ranges, not indices.



You could simply iterate over the array looking for your sentinel object object1, adding instances of NSRange to an array.



Except, that NSRange is a struct, so you can't add instances directly to NSMutableArray. There are a few options:




  1. (And this would be my first choice): Use Swift instead of Objective-C

  2. Create a custom object to wrap NSRange

  3. Use NSValue to wrap NSRange

  4. Use a C array rather than NSArray


I am going to use option 2, since option 3 involves messing around with the raw bytes of the underlying struct.



Range.h



@interface Range : NSObject

@property (readonly) NSRange range;

- (id) initWithRange: (NSRange) range
@end


Range.m



import "Range.h"



@implementation Range



- (id) initWithRange: (NSRange) range {
if (self = [super init]) {
_range = range;
}

return self;
}

@end


YourCode.m



-(NSArray *) getRanges:(NSArray *)objects delimeter: (NSObject *)object1 
{
int rangeStart = -1;
NSMutableArray *ranges = [[NSMutableArray alloc]init];
int index;

for (index=0; index<objects.count;index++) {
if (objects[index] == object1) {
if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
rangeStart = index;
}
}

if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
return ranges;
}





share|improve this answer















An NSIndexSet won't really help, since you are after ranges, not indices.



You could simply iterate over the array looking for your sentinel object object1, adding instances of NSRange to an array.



Except, that NSRange is a struct, so you can't add instances directly to NSMutableArray. There are a few options:




  1. (And this would be my first choice): Use Swift instead of Objective-C

  2. Create a custom object to wrap NSRange

  3. Use NSValue to wrap NSRange

  4. Use a C array rather than NSArray


I am going to use option 2, since option 3 involves messing around with the raw bytes of the underlying struct.



Range.h



@interface Range : NSObject

@property (readonly) NSRange range;

- (id) initWithRange: (NSRange) range
@end


Range.m



import "Range.h"



@implementation Range



- (id) initWithRange: (NSRange) range {
if (self = [super init]) {
_range = range;
}

return self;
}

@end


YourCode.m



-(NSArray *) getRanges:(NSArray *)objects delimeter: (NSObject *)object1 
{
int rangeStart = -1;
NSMutableArray *ranges = [[NSMutableArray alloc]init];
int index;

for (index=0; index<objects.count;index++) {
if (objects[index] == object1) {
if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
rangeStart = index;
}
}

if (rangeStart != -1) {
Range *range = [[Range alloc] initWithRange:NSMakeRange(rangeStart,index-rangeStart)];
[ranges addObject:range];
}
return ranges;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 6:39

























answered Nov 14 '18 at 4:33









Paulw11Paulw11

68.3k1085103




68.3k1085103













  • NSRange is not an object. It can't be added to an NSArray. You could wrap it in an NSValue, but that's cumbersome.

    – Ken Thomases
    Nov 16 '18 at 5:41



















  • NSRange is not an object. It can't be added to an NSArray. You could wrap it in an NSValue, but that's cumbersome.

    – Ken Thomases
    Nov 16 '18 at 5:41

















NSRange is not an object. It can't be added to an NSArray. You could wrap it in an NSValue, but that's cumbersome.

– Ken Thomases
Nov 16 '18 at 5:41





NSRange is not an object. It can't be added to an NSArray. You could wrap it in an NSValue, but that's cumbersome.

– Ken Thomases
Nov 16 '18 at 5:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291656%2fhow-to-create-ranges-from-nsarray-by-filtering-matching-conditions%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."