Unmanned aerial vehicle (UAV) swarm has sparked heated debate around the world in recent years. There are various stages to the battle of UAV swarm. This paper focuses on the attack task assignment after being cast or launched over the battlefield. In order to describe the process of task assignment for UAV swarm, a model introducing multiple constraints, such as limited detection range and imperfect communications, is established first. Then, inspired by contract network algorithm, we proposed a mechanism including competition for local revenue and cooperation for global revenue. And the premise of this mechanism is that UAV swarm is distributed, whose task is assigned by itself, not any other UAV. Finally, the advantages of our mechanism are illustrated by the contrast of different methods on the same issue.