I tried to push_back number to #property std::deque, but data doesn't add to deque and it empty
Code:
.h file
#interface HandTrackingViewController : CommonViewController
#property (readwrite, nonatomic) std::deque<double> vect;
#end
.mm file
#import "HandTrackingViewController.h"
#implementation HandTrackingViewController
- (void)mediapipeGraph:(MPPGraph*)graph
didOutputPacket:(const ::mediapipe::Packet&)packet
fromStream:(const std::string&)streamName {
...
...
...
for (int handIndex = 0; handIndex < multiHandLandmarks.size(); ++handIndex) {
...
for (int i = 0; i < landmarks.landmark_size(); ++i) {
self.vect.insert(self.vect.end(), {landmarks.landmark(i).x(), landmarks.landmark(i).y(), landmarks.landmark(i).z()});
}
for(int i=0; i < self.vect.size()-2; i+=3) {
NSLog(#"(%f, %f, %f)", self.vect[i], self.vect[i+1], self.vect[i+2]); // deque is empty
}
}
}
}
#end
But if you make an intermediate deque and assign it to self.vect, then it works
Replace self.vect.insert(self.vect.end(), {landmarks.landmark(i).x(), landmarks.landmark(i).y(), landmarks.landmark(i).z()}); with _vect.insert(_vect.end(), {landmarks.landmark(i).x(), landmarks.landmark(i).y(), landmarks.landmark(i).z()});. Don't use self. where it is not required.
Related
I have a class named person, with two values: age and weight.Why can't I access these two values in the main function like this:
int a=[chuck age];
int b=[chuck weight];
What is the best way to do that? Using properties is the correct way?
Header file:
#import <Foundation/Foundation.h>
#interface person : NSObject
{
int age;
int weight;
}
-(void) print;
-(void) setAge;
-(void) setWeight;
#end
Implementation file:
#import "person.h"
#implementation person
-(void) print
{
printf("Your age is %d and your weight is %d.", age, weight);
}
-(void) setAge
{
printf("Write age: ");
int v;
scanf("%d", &v);
age=v;
}
-(void) setWeight
{
printf("Write weight: ");
int g;
scanf("%d", &g);
weight=g;
}
#end
Are you working from some kind of a tutorial or book? That is an odd place to start for learning to write OS X or iOS apps.
In any case, the issue is that you've colluded getter/setter stuff with methods that implement other functionality.
I would suggest that your Person class be declared as:
#interface Person : NSObject
#property NSInteger age;
#property NSInteger weight;
#end
With Person.m:
#implementation Person
- (id) init {
self = [super init];
if (self) {
// preposterous initial values so we know if they weren't set.
_age = -1;
_weight = -1;
}
return self;
}
#end
That is, a Person only holds information about a single person. It does not do any kind of I/O, etc...
Then, your main.m would look something like:
#import <Foundation/Foundation.h>
#import "Person.h"
NSInteger ScanIntegerWithPrompt(NSString *prompt) {
printf("%s: ", [prompt UTF8String]);
int v;
scanf("%d", &v);
return (NSInteger) v;
}
int main(...) {
#autoreleasepool {
Person *p = [[Person alloc] init];
p.age = ScanIntegerWithPrompt(#"Enter age:");
p.weight = ScanIntegerWithPrompt(#"Enter weight:");
printf("Your age is %d and your weight is %d", p.age, p.weight);
}
return 0;
}
Structuring the code this way separates the Model -- the data container -- from the Control layer. There isn't much of a View layer here.
If you really wanted to keep the I/O / parse logic with the Person object, then add something like this to the Person object:
...
- (NSNumber)readIntegerWithPrompt:(NSString*)prompt
{
... same code as function above ...
}
- (void)readAgeFromStandardInput
{
self.age = [self readIntegerWithPrompt:#"Enter age: "];
}
- (void)readWeightFromStandardInput
{
self.weight = [self readIntegerWithPrompt:#"Enter weight: "];
}
...
Then you'd call those methods from your main.
Your problem is You're trying to access private age and weight ivars, which aren't accessible this way.
The good way to do this is to use ObjC properties, but this is not required for your example.
You need to create two methods to access the private ivars, call them age and weight, they should look like this in the class interface:
- (int) age;
- (int) weight;
and the implementation is:
- (int) age{
return age;
}
- (int) weight{
return weight;
}
Now in your main.m you can easily access the data needed like this:
#import <Foundation/Foundation.h>
#import "person.h"
int main(int argc, char *argV[]) {
#autoreleasepool {
person *andrew = [[person alloc]init];
[andrew setAge];
[andrew setWeight];
NSLog(#"Age:%d, Weight:%d",[andrew age], [andrew weight]);
}
return 0;
}
If you want to know how it's done with properties please let me know and I can update the answer :)
In your header file :
#property int age;
#property int weight;
I am working on a programm which lets you calculate the sides in a triangle using the Pythagoras method. I recently posted my first question about this. How can I code it so that you can also work out B or A. Here is my code so far:
PythagorasCalc.m
#import "PythagorasCalc.h"
#implementation PythagorasCalc
- (double)calculatePythagorasValue {
return sqrt(A*A+B*B);
}
//Access Code
- (double)A {
return A;
}
- (void)setA: (double)value {
if(A != value) {
A = value;
}
}
- (double)B {
return B;
}
- (void)setB: (double)value {
if(B != value) {
B = value;
}
}
- (double)C {
return C;
}
- (void)setC: (double)value {
if(C != value) {
C = value;
}
}
#end
PythagorasCalc.h
#import <Cocoa/Cocoa.h>
#interface PythagorasCalc : NSObject {
#private
int A;
int B;
int C;
}
#property (nonatomic) double A;
#property (nonatomic) double B;
#property (nonatomic) double C;
- (double)calculatePythagorasValue;
#end
AppDelegate.m
#import "PythagorasCalculatorAppDelegate.h"
#implementation PythagorasCalculatorAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
calculator = [[PythagorasCalc alloc] init];
}
- (IBAction)calculateClicked:(id)sender {
double A = _ATextField.doubleValue;
double B = _BTextField.doubleValue;
double C = _CTextField.doubleValue;
calculator.A = A;
calculator.B = B;
calculator.C = C;
_CTextField.doubleValue = [calculator calculatePythagorasValue];
}
#end
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import "PythagorasCalc.h"
#interface PythagorasCalculatorAppDelegate : NSObject <NSApplicationDelegate> {
//Public Calculator Object
PythagorasCalc *calculator;
}
#property (assign) IBOutlet NSWindow *window;
- (IBAction)calculateClicked:(id)sender;
#property (weak) IBOutlet NSTextField *ATextField;
#property (weak) IBOutlet NSTextField *BTextField;
#property (weak) IBOutlet NSTextField *CTextField;
#end
I have two properties "M" and "m", not the best coding style I know but bear with me. Assignment to these properties in the init method does not function properly. Here's the code in it's entirety:
#import "AppDelegate.h"
#interface Foo : NSObject
#property (nonatomic, assign) int M;
#property (nonatomic, assign) int m;
- (id)initWithM:(int)M m:(int)m;
#end
#implementation Foo
- (id)initWithM:(int)M m:(int)m {
if((self = [super init])) {
self.M = M;
printf("M = %d %d\n", M, self.M);
self.m = m;
printf("M = %d %d\n", M, self.M);
printf("m = %d %d\n", m, self.m);
}
return self;
}
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
Foo *f = [[Foo alloc] initWithM:2 m:1];
}
#end
And here is the output from the printf's:
M = 2 2
M = 2 1
m = 1 0
If I change "M" to "BAR" and "m" to "bar" it works as I would expect. Is there an explanation for this other than being a compiler bug?
Thanks.
#property int M;
#property int m;
both create
- (void)setM:(int)
If you really wanted to have both an m and an M property (which you definitely shouldn't) you can use
#property int M;
#property (setter = setLowerCaseM:, getter = lowerCaseM)int m;
Each time I press a button, mainController is calling [self.view addSubview: createCustomView.view]. Everything works fine here. The problem is that I need to put a tag on each subview I create in order to retrieve them later. I've already tried this :
MainController.m
NSNumber *i;
createCustomView.view.tag = i; //readonly
And what I actually wanna do is :
int i;
[createCustomView.view setTag:i];
But the setTag method doesn't exist. My question is : Is there a way I can do this other than using identifier string which brings some problems in my case?
Thanks in advance
Here's the .h file of the Controller
#import <Foundation/Foundation.h>
#import "TransactionButtonView.h"
#class TransactionButtonController;
#interface TransactionViewController : NSViewController
{
TransactionButtonController *transactionButtonController;
}
-(IBAction)createOnPushButton:(id)sender;
-(void)recalculatePositionOnRemove:(long)tag;
#property (nonatomic,assign) TransactionButtonController *transactionButtonController;
#end
Here's the .m file of the Controller
#import "TransactionViewController.h"
#import "TransactionButtonController.h"
#import "MainController.h"
#implementation TransactionViewController
#synthesize transactionButtonController;
-(IBAction)createOnPushButton:(id)sender
{
transactionButtonController = [[TransactionButtonController alloc] initWithNibName:#"TransactionButton" bundle:nil];
NSPoint originPoint;
for (int i=1; i <= [[self.view subviews]count]; i++) {
originPoint.y = transactionButtonController.view.bounds.origin.y + self.view.bounds.size.height - transactionButtonController.view.bounds.size.height*i;
transactionButtonController.view.tag = i; // Here's the PROBLEM!!!
[[transactionButtonController view]setIdentifier:[[NSNumber numberWithInt:i]stringValue]]; //here's the not good option
}
originPoint.x = transactionButtonController.view.bounds.origin.x;
[[transactionButtonController view] setFrameOrigin:originPoint];
[self.view addSubview:transactionButtonController.view];
[transactionButtonController sendVarsToButton:#"xxx" :#"591" :5 :87456356472456];
}
-(void)recalculatePositionOnRemove:(long)tag
{
NSPoint originPoint;
for (long i = tag; i<[[self.view subviews]count]; i++) {
originPoint.y = transactionButtonController.view.bounds.origin.y +self.view.bounds.size.height - transactionButtonController.view.bounds.size.height*i;
originPoint.x = transactionButtonController.view.bounds.origin.x;
[[transactionButtonController.view viewWithTag:i+1] setFrameOrigin:originPoint];
}
}
#end
If you want to add a tag to a view do this:
theView.tag = 1;
To remove it:
[[myParentView viewWithTag:1] removeFromSuperview]
Whenever I call the doSomething function my program crashes. The actual code as a bit of array bounds checking, so I know that's not an issue.
myClass.h
#import <Foundation/Foundation.h>
#interface myClass : NSObject {
BOOL **myMatrix;
}
#property(readwrite) BOOL **myMatrix;
-(myClass*)initWithWidth: (int)w andHeight: (int)h;
-(void)doSomething;
+(BOOL **) createMatrixWithHeight: (int)h andWidth: (int)w;
#end
myClass.m
#import "myClass.h"
#import <Foundation/Foundation.h>
#implementation myClass
#synthesize myMatrix;
-(myClass*)initWithWidth: (int)w andHeight: (int)h {
self = [super init];
myMatrix = [myClass createMatrixWithHeight: h andWidth: w];
return self;
}
-(void)doSomething{
myMatrix[2][2] = YES;
}
+(BOOL **) createMatrixWithHeight: (int)h andWidth: (int)w{
BOOL **newMatrix;
newMatrix = malloc(w * sizeof(BOOL *));
int i;
for(i = 0; i < w; i++){
newMatrix[i] = malloc(h * sizeof(BOOL));
}
return newMatrix;
}
#end
There must be some important difference between the code you posted and the code in your program, because I copied and pasted this in to a program and it ran fine without crashing.
So the answer to your question is this: just like in the code you posted.
This:
newMatrix = malloc(w * sizeof(BOOL *));
int i;
for(i = 0; i < w; i++){
newMatrix[i] = malloc(h * sizeof(BOOL));
}
is not a 2-dimensional array. It's an array of pointers to array.
A 2-dimensional array would be allocated as:
newMatrix = malloc(w * h * sizeof(BOOL));
See http://en.wikipedia.org/wiki/C_syntax#Multidimensional_arrays