How to skip duplicate values in the apache velocity js - velocity

I want to skip the duplicate values so using below code but it seems not working.
#set($uniqueEntities=[])
#foreach($e in $entities)
#if($foreach.index <=12)
#if($uniqueEntities != ($e.offerId))
#set($uniqueEntities=$e.offerId)
#end
$uniqueEntities,
#set($foreach.index = $foreach.index + 1)
#end
#end
Response :
OFF-56938,OFF-44046,OFF-27626,OFF-60503,OFF-49318,OFF-52824,1355738,OFF-13099,OFF-27626,OFF-11757,1355717,OFF-27305,OFF-42752
I do not want OFF-27626 to reappear. How could we fix this please?

Without actually knowing velocity.js, looking at this answer I think this might work for you
#set($uniqueEntities = [])
#foreach($e in $entities)
#if( ! $uniqueEntities.contains( $e.offerId ) )
#if( $uniqueEntities.add($e.offerId) ) #end
##note the if above is to trap a "true" return - may not be required
$e.offerId
#end
#end

Related

Added comma separated on the foreach Velocity loop

What should I added to the following foreach loop (Velocity codes) to get the final result like appNames=A,B,C
#if($approval.has())
#foreach($item in $approval.rejected)
#set($appNames =$item.appName)
#end
#end
Thanks
I'd suggest using $foreach.hasNext for a cleaner code:
#if($approval.has())
#foreach($item in $approval.rejected)
#set($appNames =$item.appName)
#if( $foreach.hasNext ),#end
#end
#end
Just simply do add separator to it in the loop and String concatenation
#if($approval.has())
#set($appNames ="")
#set($separator="")
#foreach($item in $approval.rejected)
#set($appNames =$appNames +$separator +$item.appName)
#set($separator = ",")
#end
#end
appNames= $appNames
output
appNames= A,B,C

Obj-c EXC_BAD_ECCESS error

I'm having a strange crash, at the 4th line below.
{
...
int exp = [[resourceCompletionReward objectAtIndex:experienceD] integerValue];
int xx = mySprite.x;
int yy = mySprite.y;
[self setupRisingText:exp withX:xx withY:yy];
...
}
-(void)setupRisingText:(int)risingValue withX:xx withY:yy {
...
}
When it tries to run the setupRising Text method it just crashes. Can't see anything wrong though?
What ever you do with xx and yy in setupRisingText::: they are treated as id (something like NSObject *, not exactly like that but similar)
So instead of int variables pointers to objects (any object, not just subclasses of NSObject) are accessed and dealt with. That is the default for any parameter without a type.
Use
-(void)setupRisingText:(int)risingValue withX:(int)xx withY:(int)yy {
...
}
instead.
BTW, if you used NSNumber you would have less of a problem here.
I am wondering, shouldn't your method look like this?
-(void)setupRisingText:(int)risingValue withX:(int)xx withY:(int)yy {
//NSLog(#"%d",risingValue+xx+yy);
}

problem with elseif in velocity

I have this velocity template. It works fine in one case, but doesn't in another. If I remove the elseif it works fine. Has anyone encountered this?
#if (some condition)
## Do something
#elseif
## Do something else
#end
I don't know Velocity, but normally elseif is used with a second condition.
else seems to be what you need.
#if (some condition)
## Do something
#else
## Do something else
#end
If you are using #elseif statement then you should make sure that you are checking some condition inside that #elseif statement.
Eg :-
#if (some condition)
## Do something
#elseif (some condition)
## Do something
#else
## Do something else
#end
You can refer to this:
Apache Velocity User Guide
Basically it has to go according to one of the cases below:
#if ( condition )
##do something
#end
#if ( condition )
##do something
#else
##do something
#end
#if ( condition )
##do something
#elseif ( condition )
##do something
#else
##do something
#end
(The if-elseif-....-else ladder length can be long)

Obj-C: ++variable is increasing by two instead of one

I am writing a program that asks users yes/no questions to help them decide how to vote in an election. I have a variable representing the question number called questionnumber. Each time I go through the switch-break loop, I add 1 to the questionnumber variable so that the next question will be displayed.
This works fine for the first two questions. But then it skips the third question and moves on to the fourth. When I have more questions in the list, it skips every other question. Somewhere, for some reasons, the questionnumber variable is increasing when I don't want it to.
Please look at the code below and tell me what I'm doing wrong.
Thank you!
Eli
#import "MainView.h"
#import <Foundation/Foundation.h>
#implementation MainView
#synthesize Question;
#synthesize mispar;
int conservative = 0;
int liberal = 0;
int questionnumber = 1;
- (IBAction)agreebutton:(id)sender { ++liberal; }
- (IBAction)disagreebutton:(id)sender { ++conservative; }
- (IBAction)nextbutton:(id)sender
{
++questionnumber;
switch (questionnumber)
{
case 2: Question.text = #"Congress should ...."; break;
case 3: Question.text = #"It is not fair ..."; break;
case 4: Question.text = #"There are two ..."; break;
case 5: Question.text = #"Top quality h..."; break;
default: break;
}
}
#end
It's a bit hard to read, if you can copy it exactly how it is in the implementation file and use the code sample feature for posting code snippets.
To answer the previous question
number++;
That just adds 1 to the value.
number+=anotherNumber;
That will add anotherNumber to number, and is a quick way of saying
number = number + anotherNumber;
As for your code, is there a chance that the nextButton method is being called more then once?
does ++ automatically add 1 to the variable or does it add the variable + itsself
so if questionnumber = 1 then
does ++questionumber add questionnumber + questionnumber if so it will only work the 1st time and will skip 3 so when questionnumber is 2 you would be adding questionnumber + questionnumber which = 4
I would change to questionnumber = questionnumber + 1 or if the language supports it questionnumber += 1
Where do you set the increment seed for ++ i believe this functionality is typically used with a for loop.

How can I automatically add properties in Objective-C?

When adding new properties to classes, I find myself typing the same things over and over in xcode:
add TYPE *NAME; (in .h interface)
add #property (nonatomic, retain) TYPE *NAME; (in .h)
add #synthesize NAME; (in .m)
add [NAME release]; (in .m dealloc)
(I'm in a non-garbage collected environment.)
How can I do this automatically?
That sounds about right. IIRC, the Objective-C 2.0 doc says you might be able to leave out step #1, but otherwise I don't know of any shortcuts.
You could probably write a user script to do so within Xcode. See http://www.mactech.com/articles/mactech/Vol.23/23.01/2301XCode/index.html.
According to the Developer Documentation in 64bit runtimes you can leave out step 1.
You could look at Andrew Pang's RMModelObject - I haven't used it, but it acts as a object base class that simplifies model creation.
I haven't used it, but here's some of what's highlighted in the readme:
no need to declare instance variables,
no need to write accessor methods,
free NSCopying protocol support (-copyWithZone:),
free NSCoding protocol support (-initWithCoder:, -encodeWithCoder:),
free -isEqual: and -hash` implementation,
no need to write -dealloc in most cases.
Here's another solution which I modified from
this article (also see the initial article)
The version in the blog was searching for variables outside of the variable declaration block and was matching method names too. I have done a crude fix to only search for variables before the first '}'. This will break if there are multiple interface declarations in the header file.
I set the output to "Replace Document Conents" and input as "Entire Document"
....
#!/usr/bin/python
thisfile = '''%%%{PBXFilePath}%%%'''
code = '''%%%{PBXAllText}%%%'''
selmark = '''%%%{PBXSelection}%%%'''
import re
if thisfile.endswith('.h'):
variableEnd = code.find('\n', code.find('}'))
properties = []
memre = re.compile('\s+(?:IBOutlet)?\s+([^\-+#].*? \*?.*?;)')
for match in memre.finditer(code[:variableEnd]):
member = match.group(1)
retain = member.find('*') != -1 and ', retain' or ''
property = '#property (nonatomic%s) %s' % (retain,member)
if code.find(property) == -1:
properties.append(property)
if properties:
print '%s\n\n%s%s%s%s' % (code[:variableEnd],selmark,'\n'.join(properties),selmark,code[variableEnd:])
elif thisfile.endswith('.m'):
headerfile = thisfile.replace('.m','.h')
properties = []
retains = []
propre = re.compile('#property\s\((.*?)\)\s.*?\s\*?(.*?);')
header = open(headerfile).read()
for match in propre.finditer(header):
if match.group(1).find('retain') != -1:
retains.append(match.group(2))
property = '#synthesize %s;' % match.group(2)
if code.find(property) == -1:
properties.append(property)
pindex = code.find('\n', code.find('#implementation'))
if properties and pindex != -1:
output = '%s\n\n%s%s%s' % (code[:pindex],selmark,'\n'.join(properties),selmark)
if retains:
dindex = code.find('\n', code.find('(void)dealloc'))
output += code[pindex:dindex]
retainsstr = '\n\t'.join(['[%s release];' % retain for retain in retains])
output += '\n\t%s' % retainsstr
pindex = dindex
output += code[pindex:]
print output
There is Kevin Callahan's Accessorizer. From the web page:
Accessorizer selects the appropriate
property specifiers based on ivar type
- and can also generate explicit accessors (1.0) automagically ... but
Accessorizer does much, much more ...