How to create ranges from NSArray by filtering matching conditions?
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
|
show 3 more comments
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
2
Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think anNSIndexSetis what you want. You want an array ofNSRange. 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 byobject1.
– 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
|
show 3 more comments
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
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
ios objective-c nsarray
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 anNSIndexSetis what you want. You want an array ofNSRange. 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 byobject1.
– 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
|
show 3 more comments
2
Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think anNSIndexSetis what you want. You want an array ofNSRange. 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 byobject1.
– 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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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:
- (And this would be my first choice): Use Swift instead of Objective-C
- Create a custom object to wrap
NSRange - Use
NSValueto wrapNSRange
- 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;
}
NSRangeis not an object. It can't be added to anNSArray. You could wrap it in anNSValue, but that's cumbersome.
– Ken Thomases
Nov 16 '18 at 5:41
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- (And this would be my first choice): Use Swift instead of Objective-C
- Create a custom object to wrap
NSRange - Use
NSValueto wrapNSRange
- 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;
}
NSRangeis not an object. It can't be added to anNSArray. You could wrap it in anNSValue, but that's cumbersome.
– Ken Thomases
Nov 16 '18 at 5:41
add a comment |
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:
- (And this would be my first choice): Use Swift instead of Objective-C
- Create a custom object to wrap
NSRange - Use
NSValueto wrapNSRange
- 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;
}
NSRangeis not an object. It can't be added to anNSArray. You could wrap it in anNSValue, but that's cumbersome.
– Ken Thomases
Nov 16 '18 at 5:41
add a comment |
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:
- (And this would be my first choice): Use Swift instead of Objective-C
- Create a custom object to wrap
NSRange - Use
NSValueto wrapNSRange
- 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;
}
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:
- (And this would be my first choice): Use Swift instead of Objective-C
- Create a custom object to wrap
NSRange - Use
NSValueto wrapNSRange
- 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;
}
edited Nov 16 '18 at 6:39
answered Nov 14 '18 at 4:33
Paulw11Paulw11
68.3k1085103
68.3k1085103
NSRangeis not an object. It can't be added to anNSArray. You could wrap it in anNSValue, but that's cumbersome.
– Ken Thomases
Nov 16 '18 at 5:41
add a comment |
NSRangeis not an object. It can't be added to anNSArray. You could wrap it in anNSValue, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Wouldn't your ranges be 0-2, 3-4 and 5-7? I don't think an
NSIndexSetis what you want. You want an array ofNSRange. 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