My custom class has all the same variables - ios

Below is a simplified example of my problem. I am trying to store the same object with different values. But when I set a new value, all the values change.
StopsOnRoute.h
#interface StopsOnRoutes : NSObject
#property (nonatomic) NSUInteger start_route_id;
#property (nonatomic) NSUInteger start_stop_id;
#property (nonatomic) NSUInteger start_time;
#end
FirstViewController.m
- (void)viewDidLoad
{
NSMutableArray *route1 = [[NSMutableArray alloc] init];
StopsOnRoutes *stopOnRoutes = [[StopsOnRoutes alloc] init];
int p_time = 0;
int p_route = 0;
int p_stop = 0;
while(p_time<10){
p_time = p_time + 1;
p_route = p_route + 1;
p_stop = p_stop + 1;
[stopOnRoutes setStart_time:p_time];
[stopOnRoutes setStart_route_id:p_route];
[stopOnRoutes setStart_stop_id:p_stop];
[route1 addObject:stopOnRoutes];
}
}
Unexpected output of array route1:
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
10 | 10 | 10
Expected output of array route1:
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
4 | 4 | 4
5 | 5 | 5
6 | 6 | 6
7 | 7 | 7
8 | 8 | 8
9 | 9 | 9
10 | 10 | 10

You are reusing the same object instance over and over. Create a new one each time in the loop.
while(p_time<10){
StopsOnRoutes *stopOnRoutes = [[StopsOnRoutes alloc] init];
p_time = p_time + 1;
p_route = p_route + 1;
p_stop = p_stop + 1;
[stopOnRoutes setStart_time:p_time];
[stopOnRoutes setStart_route_id:p_route];
[stopOnRoutes setStart_stop_id:p_stop];
[route1 addObject:stopOnRoutes];
}

Related

Parse input text that is located using left padding spaces

I have the following text structure. The values below JTT JNX JNA JNO belong to previous line.
9 8 11 56507785 93
JTT JNX JNA JNO
76 98
9 8 60 3269557 58
9 8 53 7269558 150
JTT JNX JNA JNO
132 71 45-7705678
9 8 62 439559 82
I'd like to parse it in order to print the corresponding values in a single line like below:
H1 H2 H3 H4 H5 JTT JNX JNA JNO
9 8 11 56507785 93 76 98
9 8 60 3269557 58
9 8 53 7269558 150 132 71 45-7705678
9 8 62 439559 82
My issue is when I use awk with FS = space (default FS) then it takes JTT as first field and JTT has 9 spaces before, so I think should be use some technique that counts how may spaces are from left until JTT JNX JNA JNO and count number of spaces from beginning until the values below JTT JNX JNA JNO in order to positionate correctly each value. I mean, 76 and 132 below JTT header, 971 below JNX, 98 below JNA and 45-7705678 below JNO.
How can this be done in awk?
$ awk --version
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2019 Free Software Foundation.
$ uname -srv
CYGWIN_NT-6.1 3.0.7(0.338/5/3) 2019-04-30 18:08
Thanks in advance.
With GNU awk (which you have) for FIELDWIDTHS:
$ cat tst.awk
BEGIN {
OFS = ","
print "H1", "H2", "H3", "H4", "H5", "JTT", "JNX", "JNA", "JNO"
}
!NF || ($1 == "JTT") { next }
!/^ / {
if (NR>1) {
print rec
}
FS = " "
$0 = $0
$1 = $1
rec = $0
}
/^ / {
FIELDWIDTHS = "12 5 5 *"
$0 = $0
$1 = $1
for (i=1; i<=NF; i++) {
gsub(/^\s+|\s+$/,"",$i)
}
rec = rec OFS $0
}
END {
print rec
}
.
$ awk -f tst.awk file
H1,H2,H3,H4,H5,JTT,JNX,JNA,JNO
9,8,11,56507785,93,76,,98
9,8,60,3269557,58
9,8,53,7269558,150,132,71,,45-7705678
9,8,62,439559,82
$ awk -f tst.awk file | column -s, -t
H1 H2 H3 H4 H5 JTT JNX JNA JNO
9 8 11 56507785 93 76 98
9 8 60 3269557 58
9 8 53 7269558 150 132 71 45-7705678
9 8 62 439559 82
Replace OFS="," with OFS="\t" or otherwise massage to suit...

How to print Tagged Pointer for NSNumber?

I read it's article and i try print Tagged Pointer for NSNumber
I try in xcode follow code:
- (NSString *)binForObjectPointer:(NSObject *)obj
{
return [self binForScalarNumber:(uintptr_t)(__bridge void *)(obj)];
}
- (NSString *)binForScalarNumber:(uintptr_t)number
{
NSString *ms = #"";
while (number)
{
if (number & 1)
ms = [ms stringByAppendingString:#"1"];
else
ms = [ms stringByAppendingString:#"0"];
number >>= 1;
}
return ms;
}
for (int i = 0; i < 10; ++i)
{
NSNumber *n = [NSNumber numberWithInt:i];
NSString *s = [self binForObjectPointer:n];
NSLog(#"%i %# %# %#",
i,
[s substringWithRange:NSMakeRange(0, s.length - 4)],
[s substringWithRange:NSMakeRange(s.length - 4, 3)],
[s substringWithRange:NSMakeRange(s.length - 1, 1)]
);
}
and get result in console
2014-09-25 16:57:46.926 AppName[3983:60b] 0 0000001111100101001010100 110 1
2014-09-25 16:57:46.930 AppName[3983:60b] 1 0000010000000101001010100 110 1
2014-09-25 16:57:46.931 AppName[3983:60b] 2 0000000001110101001010100 110 1
2014-09-25 16:57:46.932 AppName[3983:60b] 3 0000011011111101110001100 110 1
2014-09-25 16:57:46.933 AppName[3983:60b] 4 0000011011000110000110100 110 1
2014-09-25 16:57:46.934 AppName[3983:60b] 5 0000101101000110101001100 110 1
2014-09-25 16:57:46.935 AppName[3983:60b] 6 0000100110100110111010100 110 1
2014-09-25 16:57:46.936 AppName[3983:60b] 7 0000000111011010101001100 110 1
2014-09-25 16:57:46.938 AppName[3983:60b] 8 0000001100000110101010100 110 1
2014-09-25 16:57:46.939 AppName[3983:60b] 9 0000001011000110000110100 110 1
It doesn't look like in article example:
0000 0000 0000 0000 0000 0000 0011 1011
^ ^ ^ tag bit
| |
| tagged pointer class (5)
|
binary 3
When my error?

UITableView crashes because "Attempt to create two animations for cell" (iOS 7)

In an UITableView I am trying to exchange the position of two sections and to add a new row into one of them by using a batch update.
BEFORE:
+-------------------------------------+
| |
| |
| SECTION A - HEADER - TITLE |
|-------------------------------------|
| SECTION A - ROW X |
|-------------------------------------|
| SECTION A - ROW Y > |
|-------------------------------------|
| |
| |
|-------------------------------------|
| SECTION B - ROW X |
|-------------------------------------|
| |
| |
+-------------------------------------+
AFTER:
+-------------------------------------+
| |
| |
|-------------------------------------|
| SECTION B - ROW X |
|-------------------------------------|
| |
| |
| SECTION A - HEADER - TITLE |
|-------------------------------------|
| SECTION A - ROW X |
|-------------------------------------|
| SECTION A - ROW Z - NEW ROW | <---------
|-------------------------------------|
| SECTION A - ROW Y > |
|-------------------------------------|
| |
| |
+-------------------------------------+
Here is my code:
[self.tableView beginUpdates];
[self.tableView moveSection:from toSection:to]; // from: 0, to: 1
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // indexPath.section: 1, indexPath.row: 1
[self.tableView endUpdates];
But the UITableView instance crashes and outputs the following:
2014-06-29 19:45:07.486 YouTube[3312:60b] *** Assertion failure in -[_UITableViewUpdateSupport _setupAnimationsForNewlyInsertedCells], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableViewSupport.m:1173
2014-06-29 19:45:07.488 YouTube[3312:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempt to create two animations for cell'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102447495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001021a699e objc_exception_throw + 43
2 CoreFoundation 0x000000010244731a +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000101d42f19 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
4 UIKit 0x000000010101166c -[_UITableViewUpdateSupport(Private) _setupAnimationsForNewlyInsertedCells] + 7491
5 UIKit 0x000000010101aa81 -[_UITableViewUpdateSupport _setupAnimations] + 193
6 UIKit 0x0000000100e10615 -[UITableView _updateWithItems:updateSupport:] + 1639
7 UIKit 0x0000000100e0c000 -[UITableView _endCellAnimationsWithContext:] + 11615
...
Is it not possible to move a section and add a new row at the same time in a batch update?
I know this is an old question, but just ran into this myself on iOS 11.3. We have code that diffs old and new models and generates UITableView updates, and it explodes with the "more than one animation for cell" exception when a section is both moved and has any changes to its cells (insert/delete/update).
The only solution is to delete/insert the section in this case, rather than move. You could either always do this (and probably animate sub-optimally), or do what we do and track when cells change and do the move only when safe.
You should change dataSources as well.
- (void)exchangeTableSection
{
[self.tableView beginUpdates];
NSMutableArray *temp = self.data1;
self.data1 = self.data2;
self.data2 = temp;
[self.data1 insertObject:#"ROW Z - NEW ROW " atIndex:1];
[self.tableView moveSection:0 toSection:1];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (0 == section) {
return self.data1.count;
} else if (1 == section) {
return self.data2.count;
}
return 0;
}

Irregular retain counts of NSNumbers

I am using NSNumbers throughout my app (non-ARC) using different syntaxes. Just to be a little more informed, I tried to see how NSNumbers are retained depending on their initialization syntaxes. So I did the following:
NSNumber* a = #1;
NSNumber* b = [[NSNumber alloc] initWithInt:2];
NSNumber* c = [NSNumber numberWithInt:3];
NSLog(#"%d | %d | %d", a.retainCount, b.retainCount, c.retainCount);
This code fragment is executed with a button tap, and the output has perplexed me (repetitive taps) :
73 | 27 | 6
78 | 159 | 22
78 | 160 | 22
78 | 161 | 22
78 | 162 | 22
78 | 163 | 22
85 | 169 | 22
85 | 170 | 22
85 | 171 | 22
85 | 172 | 22
Now this does not really have a purpose (at least not in my case), but I would like to know how these NSNumbers get to these retain counts.
You should never use retainCount. NEVER. look here
In Objective-C, retainCount is the number which controls the lifespan of an object. The object remains alive until the retainCount turns 0, and then the object gets deallocated. This is the big picture, with many exceptions, but this is the rule that applies here.
Those retain counts mean that those numbers are used somewhere in your application. Some other objects have retained them. Since your own code does not, this means that some other system objects do.
We'll profile your application with the "Allocation" instrument, and see what it can tell us. Here is the code we'll run:
NSNumber* a = #1;
NSNumber* b = [[[NSNumber alloc] initWithInt:2] autorelease];
NSNumber* c = [NSNumber numberWithInt:3];
NSLog(#"%d | %d | %d", a.retainCount, b.retainCount, c.retainCount);
[[[UIAlertView alloc] initWithTitle:#"number b"
message:[NSString stringWithFormat:#"address: %p, retainCount: %d", b, b.retainCount] delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil] show];
This alert will tell us what is the address of the number. Instrument will let us track this object's life.
Let's choose the Debug configuration in the profile setup of our scheme. Let's check the "Record reference count" in the "Allocations" instrument options. And see what we can get.
See? This number is indeed used by many system frameworks. Now you know why it has such a big retain count :-)

iOS: Resizing and positioning a new view added to app started in landscape

I think this has probably been asked, but after reading a lot, I'm not sure I have found an answer.
When the app is rotated to landscape, I'm adding a new view to the main view. The new view is constructed and added in code like this:
UIView * newView = ....
[rootView addSubview:newView];
But because the simulator is rotated to landscape when I add the new view I get this:
+------------------------------------------------------+
+ +---------------------------------------------+ +
+ | | | +
+ | | | +
+ | newView | rootView | +
+ | (Landscape | (Landscape) | +
+ | but portrait size) | | () +
+ | | | +
+ | | | +
+ | | | +
+ | | | +
+ +---------------------------------------------+ +
+------------------------------------------------------+
So I added newView.bounds = rootView.bounds; newView.center = rootView.center; thinking that would position the newView directly over the top of the rootView, but instead I got this:
+------------------------------------------------------+
+ +---------------------------------------------+ +
+ | | +
+ | | +
+ |--------------------------+ rootView | +
+ | | (Landscape) | +
+ | newView | | () +
+ | (Landscape size, | | +
+ | but offset) | | +
+ | | | +
+ | | | +
+ +---------------------------------------------+ +
+------------------------------------------------------+
I'm trying to figure out why they are different even though I've set the bounds and centers to be the same.
Dumping out the views I get this:
rootView; frame = (0 0; 768 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x98870a0>>
newView ; frame = (-128 128; 1024 768); autoresize = W+H; layer = <CALayer: 0x6e470a0>>
So it appears that the rootView is still 768w x 1024h, just rotated by a transform, and after setting bounds and centre, the new view is 1024w x 768h and offset by 128 points.
After some more playing around, I came up with this:
// Position the new view offscreen.
CGFloat width = rootView.bounds.size.width;
CGFloat height = rootView.bounds.size.height;
newView.frame = CGRectMake(0, height, width, height);
[rootView addSubview:newView];
// Animate to the center of the view.
[UIView animateWithDuration:1.0 animations:^{
newView.frame = CGRectMake(0,0, width, height);
}];
It works, I'm just wondering if there is a better solution.

Resources